Search code examples
phpwordpresswoocommercecartproduct-quantity

Set minimum input quantity even on ajax add to cart in Woocommerce


I have added the below code on my function.php (minimum as 6 qty for every product) it is reflect on my product category page…

add_filter( 'woocommerce_quantity_input_min','woocommerce_quantity_input_min_callback', 10, 2 );
function woocommerce_quantity_input_min_callback( $min, $product ) {
    $min = 6;  
    return $min;
}

But while clicking the add to cart button (Ajax) the cart page only have 1 qty not 6 qty… How to fix this?

Need qty 6 on cart page for each product.


Solution

  • You need also to set the quantity on add to cart event (+ Ajax) and to set the input value + the minimum input value quantity at the same time. So try the following instead:

    // Set product quantity added to cart (handling ajax add to cart)
    add_filter( 'woocommerce_add_to_cart_quantity','woocommerce_add_to_cart_quantity_callback', 10, 2 );
    function woocommerce_add_to_cart_quantity_callback( $quantity, $product_id ) {
        if( $quantity < 6 ) {
            $quantity = 6;
        }
        return $quantity;
    }
    
    // Set the product quantity min value
    add_filter( 'woocommerce_quantity_input_args', 'woocommerce_quantity_input_args_callback', 10, 2 );
    function woocommerce_quantity_input_args_callback( $args, $product ) {
        $args['input_value'] = 6;
        $args['min_value']   = 6;
    
        return $args;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.