giuls22
asked 6 years ago

Is it possible to validate the woocommerce cart with the function: (`woocommerce_add_to_cart_validation`) based on the unique shop archive page the user is currently on? I am adding to the cart via AJAX.
e.g.

  • IF user is on shop page strongshop AND max item count is 20…apply rule.
  • IF user is on shop page smallshop AND max item count is 10….apply rule.

Code works when the is_page constraints are removed:

`// remove the redirect
add_filter( 'woocommerce_cart_redirect_after_error', '__return_false'); 
add_action( 'woocommerce_add_to_cart_validation', 'restrict_only_one_item_in_cart' );

 function restrict_only_one_item_in_cart($cart_item_data) {

   global $woocommerce;
   $item_count = $woocommerce->cart->cart_contents_count;

   if((is_page('strongshop') || is_page('strongshopmobile')) && $item_count >= 20){
      return false;
   }
   return $cart_item_data;
}`
Question Tags:
Powered by DW Question & Answer Pro