如何从购物车计数中删除产品或类别?
问题描述:
我想知道,在WordPress的+ WooCommerce:如何从购物车计数中删除特定的产品或类别?如何从购物车计数中删除产品或类别?
我在销售与第二个产品(订阅)关联的产品,并且当我将此产品的5个添加到Basket中时,标题中的Mini Cart显示了10个产品。
这可能会让顾客感到害怕。我看了一下类似的问题,但对于标题中的购物车不起作用。
答
您可以通过woocommerce_cart_contents_count
筛选车数量。更改slug_of_category_to_ignore
以适应。
/**
* Filters the reported number of cart items.
* Counts only items NOT in certain category.
*
* @param int $count
* @return int
*/
function so_43498002_cart_contents_count($count) {
$cart_items = WC()->cart->get_cart();
$subtract = 0;
foreach ($cart_items as $key => $value) {
if (has_term('slug_of_category_to_ignore', 'product_cat', $value['product_id'])) {
$count -= $value[ 'quantity' ];
}
}
return $count;
}
add_filter('woocommerce_cart_contents_count', 'so_43498002_cart_contents_count');
答
尝试这个
add_action('woocommerce_check_cart_items', 'woocommerce_check_cart_quantities');
function woocommerce_check_cart_quantities() {
$total_products = WC()->cart->cart_contents_count;
$multiples = 6;
$totale = 0;
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$prodotti = $values['data'];
if(! has_term(array(169, 152), 'product_cat', $prodotti->id)){
$totale += $values['quantity'];
}
}
echo $totale;
if (($totale % $multiples) > 0){
wc_add_notice(sprintf(__('You need to buy in multiples of %d products', 'your-textdomain'), $multiples), 'error');
}
}
+0
谢谢你会尝试。我承认来自另一篇文章的类别编号。 :) –
感谢您的快速答复。我明天会试试这个。 Semms在function.php –
是的,或者一个网站特定的片段插件。 – helgatheviking