phpwordpresswoocommerce

Not able to get get_term_link in product-attributes.php woocommerce


I have the below code:

<?php 
    $terms = get_the_terms( $post->ID, 'product_cat');
    foreach ($terms  as $term  ) {
        $product_cat_name .= $term->name . ', ';
    } 
?>
    
    <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
    <th class="woocommerce-product-attributes-item__label"><?php esc_html_e( 'Category', 'webinweb'); ?></th>
     <td class="woocommerce-product-attributes-item__value" ><?php echo '<a href="'.get_term_link($term->slug, $term->taxonomy).'">'.$product_cat_name.'</a>'; ?> </td>
    <tr>

The link to each product-category is set by 'get_term_slug'. But this is always the same product-category link and not the product-category link for that product.

What do I need to add to get this working?


Solution

  • <?php
    global $product;
    $prodId = $product->get_id();
    $terms = get_the_terms( $prodId, 'product_cat');
    $product_cat_name = '';
    foreach ($terms  as $term  ) {
       $product_cat_name .= '<a href="'.get_term_link($term->slug, $term->taxonomy).'">'.$term->name . '</a>';
    } 
    ?>
    <table class="woocommerce-product-attributes shop_attributes">
        <?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
            <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
            <th class="woocommerce-product-attributes-item__label"><?php esc_html_e( 'Category', 'webinweb'); ?></th>
            <td class="woocommerce-product-attributes-item__value" ><?php echo $product_cat_name; ?> </td>
         </tr>
      <?php endforeach; ?>
    </table>