我想检查项目是否已添加到购物车已经在购物车中属于哪个类别,如果该类别有超过5个产品,则抛出异常消息:
function check_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
$cat_ids = array();
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
if(in_array(434, (array)$cat_ids) || in_array(435, (array)$cat_ids)) {
//category is in cart!
$product_in_cart = true;
//count the category?
$count = 1;
while($product_in_cart === true) {
$count +=1;
}
}
}
return $product_in_cart;
return $count;
}
然后回显出错误信息无法添加到购物车。每个类别购买购物车的最佳方式是什么?基本上,我想限制项目添加到每个类别的购物车。谢谢你们