Skip to main content

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

Comments

Popular posts from this blog

IIS, There was an error when trying to connect. Do you want to retype your credentials and try again?

IIS 8 error on windows server 2012 insufficient permission There was an error when trying to connect. Do you want to retype your credentials and try again? Details: Filename: \?\C:Windows\system32\inetsrv\config\redirection.config Error: Cannot read configuration file due to insufficient permissions screenshot: Solution: Steps to short out this issue: go to C:\Windows\Microsoft.Net\Framework64\v2.0.50727\CONFIG\machine.config here you got the redirection tag like the below <configurationredirection enabled="true" password="[enc:IISCngProvider:X0ObCWwZ4+PrTHiFVPtzFeCcL8u5P6KUOYfo1/0QrgZWATA5pKWqHvD8nL2crNJKyyqr4z/rBdLPjdRcaLxAMMj4l+lvp5EXXKSXueolvyGa34F4QZfbBVCM6oVNcq3M368TOTVjJv4POVFQWvu0MDVlGgReglXB+Lw5BRI4Htw=:enc]" path="C:\Windows\System32\inetsrv\config\import\" username="Administrator"> </configurationredirection"></li"> you need to change this to <configurationRedirection /> Th...

Plesk - Upgrade the .net Framework to 4.7.2 OR 4.8

 Steps to Upgrade the .net Framework to 4.7.2 OR 4.8 1. First Download the .NET Framework from https://dotnet.microsoft.com/download/dotnet-framework   2. Install on the Server. 3. Login to the Plesk and go to "Tools & Settings" > "Server Components" and refresh the components using the refresh button 4. After update, it will reflect in "Web Script" section at the server component page. All done!!!