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


Recreated CubeWP Fields Not Showing on Frontend? Easy Fix Guide

It can be perplexing when you’ve set up your custom fields in CubeWP, they appear correctly in...

How to Stop WordPress Plugin Shortcode from Duplicating on Reload

It can be frustrating when your WordPress shortcodes duplicate content on page reload. This usually ...

Data Validation Issues using `dangerouslySetInnerHTML` into Gutenberg Blocks

Using dangerouslySetInnerHTML in React, and by extension in Gutenberg blocks, is a powerful tool but...

Fix SEO Issues: Add Trailing Slash to Category URLs Easily

To fix SEO issues by consistently adding a trailing slash to your category URLs in WordPress, the ea...

Recent Posts