Limit the number of cart items in Woocommerce

20.4k views Asked by At

I am using Woocommerce and need the following:

  1. As product is being sold to another country and that country's customs only allow a total quantity of 6, so I need to prevent customers from order more than 6 items (products).

  2. 6 is the total of items or products. Customer can order 1 product in quantity of 6 or 2 products with quantity of 3 each. Customs will only allow a total number of items to be 6.

  3. If there are more then 6 items in the cart, a warning should appear and prevent customer from proceeding to check out.

Is this possible to limit cart items to 6 and to display a message when this limit is exceeded?

2

There are 2 answers

1
helgatheviking On

You can add additional validation parameters when validating the product being added to cart. woocommerce_add_to_cart_validation expects a true or false value to be returned depending on whether the product is OK to be added to the cart:

/**
 * When an item is added to the cart, check total cart quantity
 */
function so_21363268_limit_cart_quantity( $valid, $product_id, $quantity ) {

    $max_allowed = 6;
    $current_cart_count = WC()->cart->get_cart_contents_count();

    if( ( $current_cart_count > $max_allowed || $current_cart_count + $quantity > $max_allowed ) && $valid ){
        wc_add_notice( sprint( __( 'Whoa hold up. You can only have %d items in your cart', 'your-plugin-textdomain' ), $max ), 'error' );
        $valid = false;
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'so_21363268_limit_cart_quantity', 10, 3 );
2
LoicTheAztec On

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

  • When a product is added to cart (In shop pages and product pages)
  • When quantities are updated in cart page

Using a custom function hooked in woocommerce_add_to_cart_validation filter hook, will allow you to restrict the cart items to 6 max and to display a custom message when this limit is exceeded:

// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_six_items_allowed_add_to_cart', 10, 3 );

function only_six_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 >= 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;
}

Using a custom function hooked in woocommerce_update_cart_validation filter hook, will allow you to control the cart items quantities update to your 6 cart items limit and to display a custom message when this limit is exceeded:

// Checking and validating when updating cart item quantities when products are added to cart
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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works