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:

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


Can You Customize Template Parts Per Page Template in WordPress? Yes—Here’s How

Yes, you can absolutely customize template parts per page template in WordPress! This is a common an...

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 ...

Customize WordPress User Approval Emails Without Plugins (Step-by-Step Guide)

Customizing WordPress user approval emails without plugins involves using WordPress’s built-in...

Media Gallery Preview Images Not Updating After Replacing via Media Library (WPBakery Grid Fix)

Troubleshooting Guide: WPBakery Media Grid Thumbnails Not Updating This guide will help you resolve ...

Recent Posts