WooCommerce:自定义购物车商品按产品类别计数 [英] WooCommerce: Custom cart item counts by product category

查看:61
本文介绍了WooCommerce:自定义购物车商品按产品类别计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 此脚本 这样,我就可以在WooCommerce中的购物车图标上方显示购物车内容总计:

I found this script that allows me to show cart content total above the shopping cart icon within WooCommerce:

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

如果我的购物车中有100件商品,则显示100。

If there are 100 items in my cart it shows 100.

我真正想要的是按项目类型显示计数。因此,如果有50件T恤和50件短裤,我希望将这2种类型的商品单独显示出来。

What I really want is to display the count by item types. So if there are 50 t-shirts and 50 shorts, I want that 2 item types count to be displayed individually.

有人知道我能做到这一点吗?

Does anyone knows how I can achieve this?

谢谢

推荐答案


已更新-2018年8月-轻巧紧凑的代码版本。

Updated - August 2018 - Lighter and compact code version.

类似的新答案:
Woocommerce 3中按产品类别分类的自定义购物车商品计数

此功能用于显示特定产品类别的购物车计数:

This function is made to display the cart count for a specific product category:

function cat_cart_count( $cat_name ) {
    $count = 0; // Initializing

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( has_term( $term, 'product_cat', $cart_item['product_id'] ) )
            $count += $cart_item['quantity'];
    }
    // Returning category count
    return $count == 0 ? false : $count;
}

您将上面的代码粘贴到活动子主题的function.php文件中

对于 短裤 类别,您将使用这种方式: cat_cart_count('shorts') ,并将替换 WC()-> cart-> get_cart_contents_count()


注意:自woocommerce 3起, WC()-> cart-> get_cart_url( ); 替换为 wc_get_cart_url()

tshirts short 的代码段类别:

Customizing the snippet code with it for "tshirts" and "short" categories:

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d t-shirt', '%d t-shirts', cat_cart_count( "tshirt" ) ), cat_cart_count( "tshirt" ) ); ?> - <?php echo sprintf (_n( '%d short', '%d shorts', cat_cart_count( "shorts" ) ), cat_cart_count( "shorts" ) ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

作为html标签类 cart-contents由woocommerce刷新的购物车片段使用,您可能需要按以下说明进行重命名:在Woocommerce 3中按产品类别对自定义购物车项进行计数

As the html tag class "cart-contents" is use by woocommerce refreshed cart fragments, you might need to rename it as explained in: Custom cart item counts by product category in Woocommerce 3

自定义代码段中的最后一项内容,您可以按自己的方式进行自定义添加一些文字或其他内容……我用类别名称替换了 item(s)

Last thing in our customized snippet code, you can customize-it your way adding some text or additional things… I have replace "item(s)" by the category name.

这篇关于WooCommerce:自定义购物车商品按产品类别计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆