1

so I'm trying to figure out how to show the categories under the product on this website:http://lauragdiaz.com/the-list/ The products are the brand (Alltruiest, BKIND etc.)

I tried to fix the code with this article: Get Order items and WC_Order_Item_Product in WooCommerce 3 But everytime I do it, it shows a fatal error in my code.

I'm not exactly sure what I'm doing wrong. The code I have right now which shows 'Array' is this one:

function skyverge_shop_display_skus() {

    global $product;

    if ( $product->get_category_ids() ) {
        echo '<div class="product-meta">Catégorie: ' . $product->get_category_ids() . '</div>';
    }

}


add_action( 'woocommerce_after_shop_loop_item', 'skyverge_shop_display_skus', 9 );

Where should I put the get_category_ids? and how to get the info to show?

Thank you!

Naomie
  • 11
  • 1

1 Answers1

0

Something like this would work. As I needed to display tags/categories. This also allows them to link to the term link so they could load the related products by those.

add_action('woocommerce_shop_loop_item_title', 'add_tags_and_category', 15);
function add_tags_and_category() { ?>
    <div class="tags">
    <?php $product_tags = get_the_terms( get_the_ID(), 'product_tag') ;
    if( $product_tags && ! is_wp_error( $product_tags ) ) :
        foreach( $product_tags as $tag) : ?>
            <a href="<?php echo get_term_link( $tag->slug, 'product_tag'); ?>" rel="tag" class="btn btn-primary btn-sm mb-4"><?php echo $tag->name; ?></a>
        <?php endforeach;
    endif;
    $product_terms = get_the_terms( get_the_ID(), 'product_cat');
    if( $product_terms && ! is_wp_error( $product_terms ) ) :
        foreach( $product_terms as $term) : ?>
            <a href="<?php echo get_term_link( $term->slug, 'product_cat'); ?>" rel="tag" class="btn btn-primary btn-sm mb-4"><?php echo $term->name; ?></a>
        <?php endforeach;
    endif; ?>
    </div>
<?php }

Change the HTML to suit your need. If you only need product categories. Just delete the first if

Bazdin
  • 1,049
  • 7
  • 9
  • Thank you so much it works!! Is there a way to not make them clickable (the categories/tags)? I tried to remove a=href but it ruins the code so I'm not sure... – Naomie Feb 25 '20 at 23:47
  • This should work fine. You may want to add a class to the span or something so you can allow a space between them. ```php $product_terms = get_the_terms( get_the_ID(), 'product_cat'); if( $product_terms && ! is_wp_error( $product_terms ) ) : foreach( $product_terms as $term) : ?> name; ?> ``` – Bazdin Feb 26 '20 at 14:09
  • Thank you SOOOO MUCH! really appreciate it :) – Naomie Feb 26 '20 at 14:23
  • You are most welcome. If you feel my answer completely answers your question. Please flag it as answered. – Bazdin Feb 26 '20 at 14:27