最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce Cart - Group Products By Category

programmeradmin1浏览0评论

I am trying to group my products in the woocommerce cart by category so that it display as

Category 1 - Product 1, Product 2.

Category 2 - Product 1, Product 2 etc

The loop in cart.php pulls through all products at the same time:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

is there a way to only pull through products from one category? Something like:

foreach ( WC()->cart->get_cart(['product_cat']->'football') as $cart_item_key => $cart_item ) {

And then to repeat the loop again for each of the categories?

Thanks for the help - appreciated

I am trying to group my products in the woocommerce cart by category so that it display as

Category 1 - Product 1, Product 2.

Category 2 - Product 1, Product 2 etc

The loop in cart.php pulls through all products at the same time:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

is there a way to only pull through products from one category? Something like:

foreach ( WC()->cart->get_cart(['product_cat']->'football') as $cart_item_key => $cart_item ) {

And then to repeat the loop again for each of the categories?

Thanks for the help - appreciated

Share Improve this question edited Oct 8, 2019 at 15:48 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 8, 2019 at 15:11 Matt - Salusa CommunityMatt - Salusa Community 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You need to use has_term() conditional function inside the foreach loop like:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    if( has_term( array('football'), 'product_cat', $cart_item['product_id'] ) ) {
        // Do something
    }
}
发布评论

评论列表(0)

  1. 暂无评论