I use woocommerce for my catering start-up. I work with companies that order lunches for their clients, and they have to choose the same products for each person. So, the quantities will be the same for all products.
For example, a group of 30 people want a sandwich, a soda, and a fruit => 30 sandwiches + 30 sodas + 30 fruits.
Is that a way to set the size of the group when entering the shop, so the quantities will be automatically set?
The code below will allow customers to set a specific quantity product amount for all products, through a custom form displayed by a shortcode… This will allow you to make it appear where you want and you will get that:
Once customer will set his quantity, he will be redirected to shop page and he will be notified by a custom message:
And on each single product; you will have something like:
The customer can change this quantity or reset it… Here is that code:
// Shortcode with form fields for quantity
if( ! function_exists('set_bulk_product_quantity') ) {
function set_bulk_product_quantity() {
$bulk_qty = WC()->session->get( 'bulk_qty' );
if( empty($bulk_qty) )
$bulk_qty = '0';
$label_name = __('Set Products minimal Bulk Quantity ', 'woocommerce');
$submit_button = __('Set quantity', 'woocommerce');
$reset_button = __('Reset', 'woocommerce');
$style = 'style="max-width:80px;text-align:right"';
return '<form id="bulk_qty" method="post" action="">
<p class="form-row form-row-wide" id="bulk_product_quantity_field">
<label for="bulk_qty">'.$label_name.'</label>
<input type="number" class="input-text qty text" name="bulk_qty" '.$style.' value="'.$bulk_qty.'">
<input type="submit" class="button alt" name="bulk_qty_submit" id="bulk_qty_submit" value="'.$submit_button.'">
<input type="reset" class="button alt" id="bulk_qty_reset" value="'.$reset_button.'">
</p>
</form>
<script type="text/javascript">
jQuery(function($){
if( $("input[name=bulk_qty]").val() == "")
$("input[name=bulk_qty]").attr("value", "0");;
$("#bulk_qty_reset").click( function(){
console.log($("input[name=bulk_qty]").val());
$("input[name=bulk_qty]").attr("value", "0");
$("form#bulk_qty").submit();
});
});
</script>';
}
add_shortcode( 'bulk_qty', 'set_bulk_product_quantity' );
}
// Setting the bulk quantity in session data, displaying the notice and redirecting to shop
add_action( 'template_redirect', 'special_bulk_product_quantity' );
function special_bulk_product_quantity() {
if ( isset($_POST['bulk_qty']) ){
$bulk_qty = sanitize_text_field( trim($_POST['bulk_qty']) );
if( $bulk_qty == 0 ){
$qty_display = $bulk_qty;
$bulk_qty = '';
} else {
$qty_display = $bulk_qty;
}
// Set the quantity value in customer WC_Sessions
WC()->session->set( 'bulk_qty', $bulk_qty );
// Add and display a custom notice (message)
wc_add_notice( __('The bulk quantity is now set to ', 'woocommerce') . $qty_display, 'notice' );
// Redirect to shop page
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit(); // always exit at the end
}
}
// Setting the bulk quantity amount in all products
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
if( ! is_cart() ){
$bulk_qty = WC()->session->get( 'bulk_qty' );
if( ! empty($bulk_qty) ) {
$args['input_value'] = $bulk_qty; // Start from this value (default = 1)
// $args['min_value'] = 20; // Min value (default = 0)
// $args['max_value'] = -1; // Min value (default = 0)
}
}
return $args;
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
Usage with the shortcode:
- On Wordpress page or post editor just use:
[bulk_qty]
- On PHP code use:
echo do_shortcode("[bulk_qty]");