我试图从WooCommerce的特定产品类别中获取购物车中的商品数量。
我正在为酿酒厂做一个网站。它含有酒精和非酒精产品。所有的葡萄酒都属于“葡萄酒”的主要类别。或类别ID 34,其下有许多子类别和产品。
对于任何属于此类别的东西......我需要知道此类别下购物车中有多少件物品。如果有六瓶葡萄酒,无论它们是相同的产品ID还是6种不同的产品ID。我需要从“葡萄酒”的类别中获得6个数字。或类别ID为34。
我试过这个没有成功。
我是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;
}
}
}
}
答案 0 :(得分:9)
Wordpress条件函数has_term()
接受类别ID,slug,name或该值的数组。
这是做到这一点的简单方法。您的代码将更紧凑,更轻便。您可以直接使用类别ID 34 。
这是你的功能:
4*4 + 4*4 = 32
4*4 + 5*5 = 41
5*5 + 5*5 = 50
4*4 + 6*6 = 52
5*5 + 6*6 = 61
4*4 + 7*7 = 65
6*6 + 6*6 = 72
5*5 + 7*7 = 74
4*4 + 8*8 = 80
6*6 + 7*7 = 85
5*5 + 8*8 = 89
4*4 + 9*9 = 97
7*7 + 7*7 = 98
6*6 + 8*8 = 100
5*5 + 9*9 = 106
7*7 + 8*8 = 113
4*4 + 10*10 = 116
6*6 + 9*9 = 117
5*5 + 10*10 = 125
8*8 + 8*8 = 128
7*7 + 9*9 = 130
6*6 + 10*10 = 136
4*4 + 11*11 = 137
8*8 + 9*9 = 145
5*5 + 11*11 = 146
7*7 + 10*10 = 149
6*6 + 11*11 = 157
4*4 + 12*12 = 160
9*9 + 9*9 = 162
8*8 + 10*10 = 164
5*5 + 12*12 = 169
7*7 + 11*11 = 170
6*6 + 12*12 = 180
9*9 + 10*10 = 181
4*4 + 13*13 = 185
8*8 + 11*11 = 185
7*7 + 12*12 = 193
5*5 + 13*13 = 194
10*10 + 10*10 = 200
此代码经过测试且功能齐全。
使用您的功能,例如
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
类别ID的值:34
答案 1 :(得分:0)
试试这个 我从互联网上复制了这些代码,并根据您的情况进行了更新
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;
}
答案 2 :(得分:0)
使用此代码
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');