How to Limit Cart to a Fixed Amount of Items in WordPress

WPTurbo » Snippets » How to Limit Cart to a Fixed Amount of Items in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to set a limit on the number of fixed amount items that can be added to a cart on your eCommerce site? Whether you’re dealing with a special promotion, limited stock, or simply wanting to control purchase quantities, limiting the quantity of certain products in a customer’s cart can be an invaluable feature for your online store. In this article, we’ll guide you through the steps needed to enforce a limit on cart-fixed amount items, helping you optimize your business rules and customer experience.

					function wpturbo_limit_cart_fixed_amount_items( $passed, $product_id ) {
    $limit = 3; // Set your item limit here
    $cart_contents_count = WC()->cart->cart_contents_count;
    if( $cart_contents_count >= $limit ) {
        wc_add_notice( __( 'You have reached the cart limit.', 'wpturbo' ), 'error' );
        $passed = false;
    }
    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'wpturbo_limit_cart_fixed_amount_items', 20, 2 );
				

The code begins with the definition of a new function: wpturbo_limit_cart_fixed_amount_items(). This function will limit the number of items that can be added to a user’s cart in a WooCommerce-powered WordPress website.

The function wpturbo_limit_cart_fixed_amount_items takes two parameters: $passed and $product_id. $passed typically is a boolean that determines whether an item can be added to the cart or not, and $product_id carries the unique identifier of the product being added.

Inside the function, firstly, a variable $limit is initialized and assigned a value of 3. This variable represents the number of items a user is allowed to have in their cart. You can change this to any number you wish simply by changing the value of $limit.

In the next line, we’re using the WC()->cart->cart_contents_count to count the number of items already in the cart. This is stored in the variable $cart_contents_count.

Now, we have a conditional if statement to check if the number of items in the cart ($cart_contents_count) has reached or is greater than the limit ($limit). If the condition is true, an error message is displayed using the wc_add_notice() function: ‘You have reached the cart limit’. The second parameter to this function ‘error’, indicates that this is a notice of type error.

The wc_add_notice() function is part of WooCommerce and is used to display notices to the user. Replace 'You have reached the cart limit.' with your own custome error message if you want to display a message other than ‘You have reached the cart limit’.

After displaying the error message, the $passed variable is set to false. This prevents the product from being added to cart since the limit has been reached.

If the condition is false (i.e., the cart limit has not been reached), the function skips the if condition, leaving $passed untouched (which should be true). It returns $passed.

The last line of the code uses the add_filter function to hook wpturbo_limit_cart_fixed_amount_items() to the woocommerce_add_to_cart_validation filter. This filter is used by WooCommerce to validate items being added to the cart. The parameters ’20’ and ‘2’ indicate the priority of this function and the number of arguments it accepts, respectively.

By using this function, you can enforce a fixed limit on the number of items a customer can add to their cart on your WooCommerce WordPress website.

Register an account to save your snippets or go Pro to get more features.