【问题标题】:Get WooCommerce cart items count except for a product Category获取除产品类别外的 WooCommerce 购物车项目计数
【发布时间】:2021-01-18 05:49:19
【问题描述】:

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

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

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

如何减少属于某个产品类别的商品的购物车商品总数 - 例如“盒子”?

【问题讨论】:

    标签: php wordpress woocommerce cart taxonomy-terms


    【解决方案1】:

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

    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;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    注意:要对产品标签执行相同的操作,请将'product_cat' 替换为'product_tag'

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2019-12-19
      • 2016-10-18
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 2019-06-15
      相关资源
      最近更新 更多