Reputation: 117
I'm trying to get the product category to display on the cart and checkout pages for each product added.
My php knowledge is very limited so the most dumbed down explanation would be great :)
I've had a look at the woocommerce docs and googled for the genesis connect docs but I didnt find what I was looking for.
Using Genesis Woocommerce Connect and the latest woocommerce and wordpress.
Not sure where to go from here.. :/
Upvotes: 4
Views: 21292
Reputation: 113
Another option I recently implemented in cart.php after the product name section and I wanted the categories to be separated by commas:
printf( '<br />');
$product_cats = array();
$product_terms = get_the_terms( $product_id, 'product_cat' );
if ( $product_terms && ! is_wp_error( $product_terms ) ) {
foreach ( $product_terms as $term ) {
$product_cats[] = $term->name;
}
}
// Output the categories separated by commas
echo implode( ', ', $product_cats );
Upvotes: 0
Reputation: 937
You can simply add this in your cart foreach loop
echo wc_get_product_category_list( $cart_item['product_id'] );
Checkout the cart loop here in WooCommerce Cart Github snipet.
Upvotes: 0
Reputation: 475
the woocommerce>templates>cart>cart.php is the cart page.In this,foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
loop display the products you added to the cart.Variable $product_id
of the loop have the id of each product you added the the cart.
Put this code inside the loop
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
}
echo $product_cat ;
It will display the categories. Work out and let me know:)
Upvotes: 16