Search code examples
phpwordpresswoocommerceproducttaxonomy-terms

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.


Solution

  • 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.