计算特定产品类别的购物车项目 [英] Counting cart-items of specific product category

查看:52
本文介绍了计算特定产品类别的购物车项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the number of items in the cart just from a specific product category in WooCommerce.

我正在为酿酒厂做网站。它具有酒精和非酒精产品。所有的葡萄酒都属于葡萄酒或 id类别的主要类别34,在它们的下面有许多子类别和产品。

I am doing a site for a winery. It has alcoholic and non-alcoholic products. All the wine falls under the main category of 'wine' or category id 34, with many subcategories and products underneath them.

适用于该类别下的任何东西。我需要知道此类别下的购物车中有多少物品。如果有六瓶葡萄酒,无论它们是相同的产品编号还是6个不同的产品编号。我需要从葡萄酒类别或类别ID 34中获得6。

For any thing that falls under this category... I need to know how many items are in the cart under this category. If there are six bottles of wine no matter if they are all the same product id's or 6 different product id's. I need to get that number of 6 from the category of 'wine' or category id of 34.

我尝试失败了。

我是WooCommerce的新手,还是面向对象的新手。

I am very new to WooCommerce and a little new to Object Oriented.

谢谢

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;
            }
        }
    }
}


推荐答案

Wordpress条件函数 has_term() 接受类别ID,段,名称或该值的数组。

The Wordpress conditional function has_term() accepts category ID, slug, name or an array of that values.

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

So that is the shorter and easy way to do it. Your code will be much more compact and lighter. And you can use directly your category 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;
}

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

This code is tested and fully functional.


使用功能的示例,例如使用 echo 显示 34 类别ID:

Usage of your function using for example echo to display the value for 34 category ID:

echo cat_cart_count( 34 );


这篇关于计算特定产品类别的购物车项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆