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


Unable To Update Order Meta in Woocomerce?

Having trouble updating order meta in WooCommerce?  This is a common issue that can stem from vario...

Why Apple Pay Isn’t Showing on WooCommerce Authorize.Net Checkout (SkyVerge)

It’s understandable to be frustrated when you’ve meticulously followed all the steps for...

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

How to remove admin menu pages inserted by plugins?

To remove admin menu pages inserted by plugins in WordPress, you typically use the remove_menu_page(...

Recent Posts