WooCommerce Limit Maximum Quantities From Specific Category

  • Update On February 18th, 2020
  • in WooCommerce
WooCommerce Limit Maximum Quantities From Specific Category - WooCommerce Limit Maximum Quantities From Specific Category

This post was last updated on February 18th, 2020 at 01:06 pm

A filter to limit maximum quantity can be added to the cart from a specific product category.
This code snippet goes in your functions.php file, of your active child theme (or active theme).

In the following snippet, I restrict 2 categories with maximum allowed quantities.

// Allow only pre-defined quantity from specific per category in the cart
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );
function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {

    $running_qty = 0;
    $restricted_product_cats = $restricted_product_cat_slugs = array();

    //Specify the category/categories by category slug and the quantity limit
    $restricted_product_cats[] = array('cat_slug'=> 'aluminum-pedals', 'max_num_products' => 3); // change here
    $restricted_product_cats[] = array('cat_slug'=> 'caliper-paint-kit', 'max_num_products' => 4); //change here
    foreach($restricted_product_cats as $restricted_prod_cat) $restricted_product_cat_slugs[]= $restricted_prod_cat['cat_slug'];

    // Getting the current product category slugs in an array
    $product_cats_object = get_the_terms( $product_id, 'product_cat' );
    foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;

    // Iterating through each cart item
    foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){


        // Check if restricted category/categories found
        if( array_intersect($restricted_product_cat_slugs, $current_product_cats) ) {

            foreach($restricted_product_cats as $restricted_product_cat){
                if( in_array($restricted_product_cat['cat_slug'], $current_product_cats) && has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {

                    // count(selected category) quantity
                    $running_qty += (int) $cart_item['quantity'];

                    // Check if More than allowed products in the cart
                    if( $running_qty >= $restricted_product_cat['max_num_products'] ) {

                        //limit to maximum allowed
                        WC()->cart->set_quantity( $cart_item_key, $restricted_product_cat['max_num_products'] ); // Change quantity

                        // Get the category information
                        $catinfo = get_term_by('slug', $restricted_product_cat['cat_slug'], 'product_cat');

                        wc_add_notice( sprintf( 'Only %s '.($restricted_product_cat['max_num_products']>1?'products from this category ('.$catinfo->name.') are':'product from this category is').' allowed in the cart.',  $restricted_product_cat['max_num_products'] ), 'error' );
                        $passed = false; // don't add the new product to the cart
                        // We stop the loop
                        break;
                    }
                }
            }
        }
    }
    return $passed;
}

 

This code is tested on WooCommerce version 3.0+. Let me know in the comments if you find this code snippet useful or have any questions!.

 

 

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you...read more about me

1 Comment

You can post comments in this post.


  • Thank you so much for the code, but I am having an issue making it work with more than one category. While I am no PHP programmer, something just seems odd to me (again, not a PHP programmer).

    1) running_qty doesn't seem to be reset back to zero. If we walked through the cart for the first category type counting, then when we walk through again, we'd need qty reset back to zero. That might be one of the causes of why it works for one category, but not another.
    2) restricted_products_cat seems to be a single array var and not an array of restricted categories… could be totally wrong. I might be completely off on this one.

    i'm messing around with the code trying to figure it out. Am I barking up the wrong tree. Why is it not working for two categories.

    For instance, suppose a parent category of "Tires". Then under there I had sub-categories of "Truck Tires", "Car Tires", "SUV Tires", etc. In each sub category (e.g. "Truck Tires"), I had multiple brands. I want to limit them to 4 total tires based on the "Truck Tires" category. I don't care the mix, just no more than 4. Then, I want to limit them to say 2 tires under "SUV Tires". Bad example, but hopefully makes sense.

    Tobin 4 years ago Reply


Leave A Reply