php - How to limit item in the cart for each category in woocommerce -


hi have visual images, first click add-to-cart button, check validation of qty allowance each category, output error message on top of cart widget. how count product in each cateogy in cart woocommerce? in advance

public function check_product_in_cart($product_in_cart) {             //check see if user has product in cart             global $woocommerce;              // start of loop fetches cart items              foreach ( $woocommerce->cart->get_cart() $cart_item_key => $values ) {                 $_product = $values['data'];                  $terms = get_the_terms( $_product->id, 'product_cat' );                  // second level loop search, in case items have several categories                 // started editing guillaume's code                  $cat_ids = array();                  foreach ($terms $term) {                     $cat_ids[] = $term->term_id;                 }                  if(in_array(70, (array)$cat_ids) ||in_array(69, (array)$cat_ids) || in_array(68, (array)$cat_ids)) {                     //if exist in "strain" category                       //output true                     $product_in_cart = true;                    //how count item in category???                 }              }              return $product_in_cart;          } 

here how got it, count quantity of category inside cart item. first looking if there item in cart belong category, if found, quantity of item , output category count. work every single category.

    public function check_product_in_cart($product_in_cart) {         //check see if user has product in cart         global $woocommerce;          // start of loop fetches cart items         $aty = 0;         foreach ( $woocommerce->cart->get_cart() $cart_item_key => $values ) {             $_product = $values['data'];              $terms = get_the_terms( $_product->id, 'product_cat' );              // second level loop search, in case items have several categories             // started editing guillaume's code              $cat_ids = array();              foreach ($terms $term) {                 $cat_ids[] = $term->term_id;             }              if(in_array(70, (array)$cat_ids) ||in_array(69, (array)$cat_ids) || in_array(68, (array)$cat_ids)) {//popular                //category in cart!                 $product_in_cart = true;                $product_catqty = $values['quantity'];//get quantiy of item                $aty += $product_catqty;             }             $cat_qty =  $aty;         }           return $cat_qty;     } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -