John Davidson

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


I have the below code:

$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>

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?

My Answer:
To get the term link in the product-attributes.php file in WooCommerce, you can use the get_term_link() function. Here is an example of how you can use it in your code:

$terms = wp_get_post_terms( $product->get_id(), $attribute['name'], 'all' );

foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
echo '' . $term->name . '';

This code will retrieve the terms for the specified attribute of the product and then generate a link for each term using the get_term_link() function. Make sure to replace `$product->get_id()` with the appropriate product ID and `$attribute['name']` with the attribute name you want to get the terms for.

Let me know if you need any further assistance.

Rate this post

3 of 5 based on 3781 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us