嗨,我有一个视觉图像, 首先单击“添加到购物车”按钮,检查每个类别的数量限额验证,在购物车小部件顶部输出错误消息。 我如何使用woocommerce计算购物车中每个cateogy的产品? 提前致谢
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 :(得分: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;
}