Ecommerce Shopify WordPress Discussion

How to display WooCommerce product attributes term names as linked terms?

I want to show product attributes in a single product tab, but as links to products with this attribute. I found a code like this: function cw_woo_attribute(){ global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $display_result = ''; foreach ( $attributes as $attribute ) { if ( $attribute->get_variation() ) { continue; } $name = $attribute->get_name(); if ( $attribute->is_taxonomy() ) { $terms = wp_get_post_terms( $product->get_id(), $name, 'all' ); $cwtax = $terms[0]->taxonomy; $cw_object_taxonomy = get_taxonomy($cwtax); if ( isset ($cw_object_taxonomy->labels->singular_name) ) { $tax_label = $cw_object_taxonomy->labels->singular_name; } elseif ( isset( $cw_object_taxonomy->label ) ) { $tax_label = $cw_object_taxonomy->label; if ( 0 === strpos( $tax_label, 'Product ' ) ) { $tax_label = substr( $tax_label, 8 ); } } $display_result .= $tax_label . ': '; $tax_terms = array(); foreach ( $terms as $term ) { $single_term = esc_html( $term->name ); array_push( $tax_terms, $single_term ); } $display_result .= implode(', ', $tax_terms) . '<br />'; } else { $display_result .= $name . ': '; $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />'; } } echo $display_result; } add_action('woocommerce_single_product_summary', 'cw_woo_attribute', 25); And it works! But how can this code be modified so that the names of individual attributes are a link to all products that have that attribute assigned? I previously used 'WooCommerce Show Attributes' by Isabel Castillo, but it started to conflict with something and displays an error on the add to cart button.
Note that the code you are using can be highly simplified. Using WordPress get_term_link() will allow you to display linked term names when possible. Try the following: add_action( 'woocommerce_single_product_summary', 'woocommerce_single_product_linked_attributes', 25 ); function woocommerce_single_product_linked_attributes(){ global $product; $attributes = $product->get_attributes(); $html_output = ''; if ( empty($attributes) ) { return; } foreach ( $attributes as $attribute ) { if ( $attribute->get_variation() ) { continue; } if ( $attribute->is_taxonomy() ) { $values = array(); $taxonomy = $attribute->get_taxonomy(); foreach ( $attribute->get_terms() as $term ) { $term_link = get_term_link($term, $taxonomy); // Get term link if ( ! is_wp_error( $term_link ) ) { $values[] = '<a class="attr-link" href="'. esc_url( $term_link ) . '">'. esc_html( $term->name ) . '</a>'; } else { $values[] = esc_html( $term->name ); } } $html_output .= wc_attribute_label($taxonomy) . ': ' . implode(', ', $values) . '<br>'; } else { $html_output .= $attribute->get_name() . ': ' . esc_html( implode(', ', $attribute->get_options()) ) . '<br>'; } } echo $html_output; } Code goes in function.php file of your active child theme (or active theme). Tested and works.

January 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.