【问题标题】:Counting cart-items of specific product category计算特定产品类别的购物车项目
【发布时间】:2016-12-07 19:12:21
【问题描述】:

我正在尝试仅从 WooCommerce 中的特定产品类别获取购物车中的商品数量。

我正在为一家酿酒厂做一个网站。它有酒精和非酒精产品。所有葡萄酒都属于“葡萄酒”或类别 id 34 的主要类别,其下还有许多子类别和产品。

对于属于此类别的任何物品...我需要知道此类别下的购物车中有多少物品。如果有六瓶酒,无论它们是相同的产品 ID 还是 6 个不同的产品 ID。我需要从 'wine' 类别或类别 id 34 中获得 6。

我试过这个没有成功。

我对 WooCommerce 很陌生,对面向对象还有些陌生。

谢谢

function cat_cart_count( $cat_name ) {

// $cat_name variable is for you normally "tshirts" or "shorts"

global $woocommerce; $cat_count = 0; 

// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
    $_product_id = $values['product_id']; // product ID
    $_product_qty = $values['quantity']; // product quantity

    // Getting categories of the product (could be more than one)
    $terms = get_the_terms( $_product_id, 'product_cat' );

    // Checking this product has a category
    if ( $terms && ! is_wp_error( $terms ) ) { 
        $term_name = array();

        // For each category of that product
        foreach($terms as $term) {

            // Adding the category name to an array
            $term_name[] = $term->name;

            // if the product has $cat_name category
            if ( in_array( $cat_name, $term_name ) ) {

                // add 1 x product quantity to the count
                $cat_count =+ 1 * $_product_qty;
            }
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart custom-taxonomy


    【解决方案1】:

    Wordpress 条件函数 has_term() 接受类别 ID、slug、名称或这些值的数组。

    所以这是更短更简单的方法。您的代码将更加紧凑和轻便。您可以直接使用您的类别 ID 34。

    这是你的功能:

    function cat_cart_count( $cat_name ) {
        $cat_count = 0; 
        
        // Iterating through each cart item
        foreach(WC()->cart->get_cart() as $cart_item)  
            if( has_term( $cat_name, 'product_cat', $cart_item['product_id']))
                $cat_count += $cart_item['quantity'];
                
        return  $cat_count;
    }
    

    此代码经过测试且功能齐全。

    使用您的函数,例如使用 echo 来显示 34 类别 ID 的值:

    echo cat_cart_count( 34 );
    

    【讨论】:

      【解决方案2】:

      试试这个 我从互联网上复制了这段代码并进行了更新以适合您的情况

      function check_product_in_cart() {
              global $woocommerce;
                  //  id of targeted category is 5 for example
      
              $product_count = 0;
      
              // 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
                  foreach ($terms as $term) {
                      $_categoryid = $term->term_id;
                      if (( $_categoryid === 5 ) {
      
                          $product_count=+ 1 * $_product_qty; ;
                      }
                  }
              }
      
              return $product_count;
         }
      

      【讨论】:

      • 感谢您的回复。这个 check_product_in_cart() 的括号中需要加入类别 id 吗?
      • 如果你想让它成为动态的,只需声明这样的方法 check_product_in_cart($cat_id) 并将方法内的数字 5 替换为 $cat_id。并在需要使用方法的地方写上check_product_in_cart(5)。
      【解决方案3】:

      使用此代码

      function cat_cart_count( $cat_name ) {
          $cat_count = 0; 
          
          // Iterating through each cart item
          foreach(WC()->cart->get_cart() as $cart_item)  
              if( has_term( $cat_name, 'product_cat', $cart_item['product_id']))
                  $cat_count += $cart_item['quantity'];
                  
          return  $cat_count;
      }
      echo cat_cart_count('all-friends');
      

      【讨论】:

        猜你喜欢
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 2019-01-17
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多