2015-05-29 2 views
1

Привет, у меня есть визуальные образы, Кнопка первого щелчка по кнопке «Отправить другу», проверьте валидацию размера qty для каждой категории, выведите сообщение об ошибке поверх виджета корзины , Как подсчитать товар в каждой категии в корзине с помощью woocommerce? Заранее спасибоКак ограничить товар в корзине для каждой категории в woocommerce

public function check_product_in_cart($product_in_cart) { 
      //Check to see if user has product in cart 
      global $woocommerce; 

      // start of the loop that fetches the cart items 

      foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 

       $terms = get_the_terms($_product->id, 'product_cat'); 

       // second level loop search, in case some items have several categories 
       // this is where I started editing Guillaume's code 

       $cat_ids = array(); 

       foreach ($terms as $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 to count all item in this category??? Thanks 
       } 

      } 

      return $product_in_cart; 

     } 

ответ

0

Вот как я получил его, подсчитывают количество категории внутри тележки пункта. Сначала, если есть какие-либо предметы в корзине, относятся к этой категории, если они найдены, получают количество этого предмета и выводят количество категорий. Работайте с каждой отдельной категорией.

public function check_product_in_cart($product_in_cart) { 
     //Check to see if user has product in cart 
     global $woocommerce; 

     // start of the loop that fetches the cart items 
     $aty = 0; 
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      $_product = $values['data']; 

      $terms = get_the_terms($_product->id, 'product_cat'); 

      // second level loop search, in case some items have several categories 
      // this is where I started editing Guillaume's code 

      $cat_ids = array(); 

      foreach ($terms as $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 is in cart! 

       $product_in_cart = true; 
       $product_catqty = $values['quantity'];//get the quantiy of the item 
       $aty += $product_catqty; 
      } 
      $cat_qty = $aty; 
     } 


     return $cat_qty; 
    } 
Смежные вопросы