1
votes

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

  1. I have edited 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 ?

  1. 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?

1
It's just like if you are making a cart item loop o an existing cart item loop… You should give the complete template custom code in your question, as for know is difficult to understand where is located that code. Also it seems that you are using plugins that are making customizations. You should also give the woocommerce version that you are using.LoicTheAztec
i edited my question is that better, need anything more ?g3ar
I have answered your questionLoicTheAztec

1 Answers

1
votes

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…