Limit total quantity globally for specific product category in WooCommerce

To limit the total quantity globally for a specific product category in WooCommerce, you can use a custom function in your theme’s functions.php file. The function will check the total quantity of products in the cart that belong to a specific category and restrict customers from adding more than the defined limit.

Steps to Implement the Global Quantity Limit for a Category:
Identify the product category slug for which you want to limit the quantity.
Define the maximum quantity allowed for that category.
Use WooCommerce’s woocommerce_add_to_cart_validation hook to restrict users from exceeding the set limit.
Code Snippet:
Add this code to your functions.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function limit_category_quantity_globally($passed, $product_id, $quantity, $variation_id = null, $variations = null) {
    // Define the category slug and max quantity
    $category_slug = 'your-category-slug'; // Replace with your category slug
    $max_quantity = 10; // Set the max quantity allowed for the category
 
    // Get the cart contents
    $cart = WC()->cart->get_cart();
    $category_count = 0;
 
    foreach ($cart as $cart_item) {
        $product = wc_get_product($cart_item['product_id']);
         
        // Check if the product belongs to the specified category
        if (has_term($category_slug, 'product_cat', $product->get_id())) {
            $category_count += $cart_item['quantity'];
        }
    }
 
    // Check if adding the new quantity exceeds the limit
    if (has_term($category_slug, 'product_cat', $product_id)) {
        if (($category_count + $quantity) > $max_quantity) {
            wc_add_notice("You can only purchase up to {$max_quantity} items from the {$category_slug} category.", 'error');
            return false;
        }
    }
 
    return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'limit_category_quantity_globally', 10, 5);

How It Works:
It checks all products in the cart that belong to the specified category.
If the total quantity (including the new item being added) exceeds the limit, it prevents the addition and displays an error message.
The category slug and maximum allowed quantity can be customized as needed.

Customization:
Replace ‘your-category-slug’ with the actual slug of your product category.
Change $max_quantity = 10; to the desired limit.

This method ensures that customers cannot exceed a set quantity for a specific category across their entire cart. 🚀 Let me know if you need further modifications!

Related Posts


Ajax search functionality in WordPress

Implementing AJAX search functionality in WordPress enhances user experience by enabling live search...

WordPress Redirect Issues Across Multiple Websites

Experiencing redirect loops or unwanted redirects on multiple WordPress sites? Here’s a guide to d...

How to Retrieve and Expose Gutenberg & Theme-Generated CSS in WordPress

If you want to extract the CSS generated by Gutenberg and your WordPress theme, you can do so using ...

WordPress Force HTTPS Without Plugin: The Ultimate Method

Hey there! Are you running a WordPress website on WP Engine and want to make it more secure? Worried...