WooCommerce Limit Maximum Quantities From Specific Category

WooCommerce Limit Maximum Quantities From Specific Category - WooCommerce Limit Maximum Quantities From Specific Category

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

Leave A Reply