2

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

1 Answer 1

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
    }
}
3
  • Thanks very much , greatly appreciated Oct 11, 2019 at 9:34
  • @Matt-SalusaCommunity If this answer answers your question, you could please accept the answer, Thank you. Oct 11, 2019 at 14:01
  • That really helped me! Thanks a lot
    – Assaf Katz
    Dec 19, 2019 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.