获取除产品类别外的 WooCommerce 购物车项目数

问题描述

我可以通过以下方式获得 WooCommerce 购物车内容计数:

add_action('fl_body_open','cat_total_count');
function cat_total_count() {

echo WC()->cart->get_cart_contents_count();
}

如何减少属于特定产品类别(例如“盒子”)的商品的购物车商品总数?

解决方法

您可以使用 Wordpress has_term() 条件函数从自定义购物车项目计数中排除产品类别术语,如下所示:

primary_color_name

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。

注意:要对产品标签执行相同的操作,请将 add_action('fl_body_open','custom_total_cart_items_count'); function custom_total_cart_items_count() { // Below your category term ids,slugs or names to be excluded $excluded_terms = array('box'); $items_count = 0; // Initializing // Loop through cart items foreach ( WC()->cart->get_cart() as $item ) { // Excluding some product category from the count if ( ! has_term( $excluded_terms,'product_cat',$item['product_id'] ) ) { $items_count += $item['quantity']; } } echo $items_count; } 替换为 'product_cat'