In woocommerce, I would like to set a minimum quantity for specific products in cart. If value is less, then an error appears.
For example: Customer can checkout only if product a, b, c quantity in cart is 2 or more.
Otherwise error appears - "Minimums order quantity for every item is 2".
There are similar topics in this forum, and I thought this code could be useful.
add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min',10, 2 );
function woocommerce_quantity_input_min( $min, $product ){
if ( $product-> ) {
$min = 2;
} else {
$min = 3;
}
return $min;
}
Can someone make this work?
My real needs: In my store I sell many watches. There are 6 product categories:
I need a condition where a customer can only checkout if every product quantity in cart page is at least 2, regardless of which category it would be.
Note that we offer to add products form archive page and single product page. If added from archive page then default value is 1.
Update 2 (It works too now for archive pages and on cart for product variations).
The following code will set a minimal quantity for specific product categories:
// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {
## ---- Your settings ---- ##
$product_categories = array('Burberry', 'Daniel Wellington',
'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');
$quantity = 2;
## ---- The code: set minimun quantity for specific product categories ---- ##
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $product_categories, 'product_cat', $product_id ) ){
$args['min_value'] = $quantity;
}
return $args;
}
// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product ) {
// Only for non variable products
if( $product->is_type( 'variable' ) ) return $button; // Exit
## ---- Your settings ---- ##
$product_categories = array('Burberry', 'Daniel Wellington',
'Diesel', 'Emporio Armani', 'Marc Jacobs', 'Michael Kors');
$quantity = 2;
## ---- The code: set minimun quantity for specific product categories ---- ##
if( has_term( $product_categories, 'product_cat', $product->get_id() ) ){
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
$button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
return $button;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
For product tags, you will have just to change 'product_cat' by 'product_tag' in the code.
ADDITION: For product IDS (array of IDs)
To make it work for product Ids instead use:
// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_input_args', 20, 2 );
function min_qty_input_args( $args, $product ) {
## ---- Your settings ---- ##
$product_ids = array('23', '52', '75', '87', '90', '102');
$quantity = 2;
## ---- The code: set minimun quantity for specific product Ids ---- ##
if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
$args['min_value'] = $quantity;
}
return $args;
}
// On archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'min_qty_loop_add_to_cart_button', 50, 2 );
function min_qty_loop_add_to_cart_button( $button, $product ) {
// Only for non variable products
if( $product->is_type( 'variable' ) ) return $button; // Exit
## ---- Your settings ---- ##
$product_ids = array('23', '52', '75', '87', '90', '102');
$quantity = 2;
## ---- The code: set minimun quantity for specific product IDs ---- ##
if( in_array( $product->get_id(), $product_ids ) ){
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
$button = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
return $button;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.