Reputation: 63
Im using Woocommerce 3.3.3. and Visual Products Configurator 4.0
In My web site you can add some product to cart and proceed to checkout
cart/cart.php
template to show my product category name in cart (from line 75 to 79).The code
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
}
echo $product_cat ;
Is that placement fine ?
I have edited checkout/review-order.php
to show my category name in checkout (from line 36 to 41):
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
}
echo $product_cat ;
my category name is showed twice. How can i fix that ?
After that i can see my category name under checkout but it is displayed twice.
How can avoid this displaying repetition?
Is the placement fine?
Upvotes: 1
Views: 3180
Reputation: 253901
To display product categories inline linked names use
wc_get_product_category_list()
.
For point 1:
You should replace your code by the following dedicated function (much more compact):
echo wc_get_product_category_list( $cart_item['product_id'] );
For point 2:
You should replace your code by the following to avoid repetitions and bad formatted html (on template checkout/review-order.php
file):
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
?>
<tr class="product-categories">
<td colspan="2"><?php echo wc_get_product_category_list( $cart_item['product_id'] ); ?></td>
</tr>
<?php
Don't forget that you are adding an output inside an html table…
Upvotes: 1