Set minimum order quantity on a per-product basis in WooCommerce

We ran into a problem recently with a new product that could only be sold in boxes of 8 units, but were tracked in the supplier’s inventory as single units.

We couldn’t create a new SKU, because our stock levels would quickly go out of sync. The best solution was to set a minimum order quantity of 8, which can be done in WooCommerce by adding the following code to your theme’s Functions.php file

// Set minimum order on single product pages
add_filter( ‘woocommerce_quantity_input_args’, ‘lw_min_max_order_quantity’, 20, // Set minimum order on single product pages
add_filter( 'woocommerce_quantity_input_args', 'lw_min_max_order_quantity', 20, 2 );
function lw_min_max_order_quantity( $args, $product ) {

//The ID or IDs of products to which this rule should be applied.
$product_ids = array('42058', '42065');

$quantity = 8;

if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
$args['min_value'] = $quantity;

//Because the product must be sold in a multiple of eight, we also need to set a maximum order quantity of 8.
$args['max_value'] = $quantity;
}

return $args;
}

// On product archive pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'lw_min_max_order_quantity_archive', 50, 2 );
function lw_min_max_order_quantity_archive( $button, $product ) {
// Only for non variable products
if( $product->is_type( 'variable' ) ) return $button; // Exit

//The ID or IDs of products to which this rule should be applied.
$product_ids = array('42058', '42065');

$quantity = 8;

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;
}

There’s two functions here. The first one applies the rule to single product pages, while the second one applies the rule to the “Add To Cart” buttons on product archive pages.

The product IDs can be found by hovering over the product in the WooCommerce Products list.

Shane Gowland

Hi! I’m a web designer, software developer, and digital marketer based in South Australia. I've been building websites on WordPress for over 12 years. You can learn more about the projects and businesses I'm involved with on my website.

3 thoughts on “Set minimum order quantity on a per-product basis in WooCommerce”

  1. Hi, thanks for this. I intended to do the same but with a minimum of 2 units. I changed the $product_ids to my own and $quantity = 2 and that’s about it. The issue I have is the number of units is not present when adding to cart (but it automatically adds 2 units as the price doubles). Also, due to this, the buyer cannot increase and buy more units (Ex, 2,3,4…..). Any help would be greatly appreciated.

    Reply

Leave a Reply