更新时间:2023-02-19 17:58:52
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 for34
category ID:
echo cat_cart_count( 34 );