Wed
Wed

Reputation: 342

Set custom Add to cart default quantity except on Woocommerce cart page

I'm using this filter below to to set default quantity of 2 products to be predefined when adding to cart by default. It actually works on product page and defaults quantity setting to 2 and adds 2 products to Cart. But the problem appears when user comes to Cart page, if he/she added 4 products, all the calculations are done correctly EXCEPT displayed quantity is 2. Even if I change quantity on Cart page to 6 for example, and refresh Cart, all the amounts are recalculated correctly except displayed quantity is shown as 2. I figure I should somehow apply this filter to Add to cart button only, but don't know how.

I'd appriciate the help.

add_filter( 'woocommerce_quantity_input_args', 'rb_woocommerce_quantity_changes', 10, 2 );
function rb_woocommerce_quantity_changes( $args, $product ) {
   $args['input_value'] = 2;
   $args['max_value'] = 12;
   $args['min_value'] = 1;
   $args['step'] = 1;
   return $args; 
}

Upvotes: 1

Views: 2774

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253814

Try the following using is_cart() on 'input_value' argument:

add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {

   if( ! is_cart() ) {
       $args['input_value'] = 2; // Not on cart page
   }
   $args['max_value'] = 12;
   $args['min_value'] = 1;
   $args['step'] = 1;

   return $args; 
}

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

Related documentation: Woocommerce conditional tags

Upvotes: 4

Related Questions