Saturday, December 23, 2017

Limit the number of cart Items in Woocommerce

There is 2 actions to check and to control if you want to limit cart items:

  1. Add to basket - When product  is added to cart
  2. Update Basket - When quantities are updated in cart page
Add a custom function hooked in woocommerce_add_to_cart_validation filter hook in function.php file of your active child theme (or theme), will allow customer to restrict the cart items to 'n' max and to display a custom message when this limit is exceeded:

  1. Checking and validating when products are added to cart:
    • Restrict Total Transaction Items:
      add_filter( 'woocommerce_add_to_cart_validation', 'only_n_items_allowed_add_to_cart', 10, 3 );
      function only_n_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {
      $cart_items_count = WC()->cart->get_cart_contents_count();
      $total_count = $cart_items_count + $quantity;

      if( $cart_items_count >= n || $total_count > n ){
      // Set to false
      $passed = false;
      // Display a message
      wc_add_notice( __( "You can’t have more than n items in cart", "woocommerce" ), "error" );
      }
      return $passed;
      }
    • Restrict Item in a transaction:
      add_filter( 'woocommerce_add_to_cart_validation', 'only_five_items_allowed_add_to_cart', 10, 3 );
      function only_five_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {

      //$cart_items_count = WC()->cart->get_cart_contents_count();

      foreach ( WC()->cart->get_cart() as $cart_item ) {
      if($cart_item['product_id'] == $product_id ){
      $cart_items_count = $cart_item['quantity'];
      break; // stop the loop if product is found
      }
      }

      $total_count = $cart_items_count + $quantity;

      if( $cart_items_count > 3 || $total_count > 3 ){
      // Set to false
      $passed = false;
      // Display a message
      wc_add_notice( __( "You can’t add more than 3 items in cart", "woocommerce" ), "error" );
      }
      return $passed;
      }

  2. Checking and validating when updating cart item quantities when products are added to cart
    • Restrict Total Transaction Items:
      add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
      function only_six_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {

      $cart_items_count = WC()->cart->get_cart_contents_count();
      $original_quantity = $values['quantity'];
      $total_count = $cart_items_count - $original_quantity + $updated_quantity;

      if( $cart_items_count > 6 || $total_count > 6 ){
      // Set to false
      $passed = false;
      // Display a message
      wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
      }
      return $passed;
      }
    • Restrict Item in a transaction:
      add_filter( 'woocommerce_update_cart_validation', 'only_five_items_allowed_cart_update', 10, 4 );
      function only_five_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {

      $cart_items_count = WC()->cart->get_cart_contents_count();
      $original_quantity = $values['quantity'];
      $total_count = $cart_items_count - $original_quantity + $updated_quantity;

      if( $updated_quantity > 3 ){
      // Set to false
      $passed = false;
      // Display a message
      wc_add_notice( __( "You can’t have more than 3 items in cart", "woocommerce" ), "error" );
      }
      return $passed;
      }

No comments: