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


Ajax search functionality in WordPress

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

Solving CubeWP Custom Field Display Issues

Troubleshooting CubeWP Custom Fields Not Showing on Single Post Frontend It sounds like you’re...

How to get list of ALL wordpress tables in the database?

To get a list of ALL WordPress tables in your database, including core tables, plugin tables, and an...

CSS in WordPress: Single Stylesheet vs. Page-Specific Files – What’s Best?

WordPress developers often face the dilemma of choosing between a single consolidated CSS file or mu...

Recent Posts