I’m trying to display a simple list of all the categories within a new order that comes through, and the number of items in the order within that category. So something along the lines of:
Spray Cans – 3
Accessories – 2
Markers – 5
etc
This is what I’ve tried so far but obviously it just shows the total of products in the category and not just relating to this order.
foreach( $order->get_items() as $items ) {
$terms = wp_get_post_terms( $items->get_product_id(), 'product_cat' );
foreach( $terms as $term ) {
echo '<li>';
echo $term->name;
echo ' - ';
echo $term->count;
echo '</li>';
echo $term = count( $order->get_items() );
}
}
2
Answers
Here is the final code that worked for me if anyone needs it.
The following will list the product categories within the WC Order with the count for each of them and at the end you will have the count of order items:
Tested and works.