Limiting Product qty for a category
-
i have a category called balloon weights on my site, there are 10 weight products in the category, all these weights come in boxes, some of the weights are very heavy so we have to limit the amount ordered per customer order so I am trying to limit three of the products in the balloon weights category “Foil Balloon Weights – 170g (12ct)”, “Bubble Balloon Weights – 35g (25ct)” and “Foil Balloon Weights – 160g (12ct)” you can have any combination of the three but you cant have more than 12 total but only regarding these three products, you should still be able to order other weights, these are also variation products as they come in different colours i dont know if that makes a difference yet.
i have code that limits the balloon weights category to a max of 12 per order but the issue obviously is it limits all the weights not just three, furthermore its buggy so it only checks the cart to ensure the max limit has not been reached, it does not check how much your actually adding to the cart
// 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'=> 'balloon-weights', 'max_num_products' => 12); // 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; }
- The topic ‘Limiting Product qty for a category’ is closed to new replies.