Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

Show specific product attributes on single product page (WooCommerce)


I would like to show specific attributes on single product page on WooCommerce.

I'm using Get specific product attribute and display it in Woocommerce Single Product Pages answer code

The question is: how i could add more attributes in the existing code?


Solution

  • Here is how to add more attributes

    add_action( 'woocommerce_single_product_summary', 'product_attribute_dimensions', 45 );
    function product_attribute_dimensions(){
        global $product;
    
        $taxonomies = array('pa_size','pa_color'); //add your attributes
        //Then we loop each of them
        foreach($taxonomies as $taxonomy){
            $value = $product->get_attribute( $taxonomy );
            if ( $value ) {
                $label = get_taxonomy( $taxonomy )->labels->singular_name;
        
                echo '<p>' . $label . ': ' . $value . '</p>';
            }
        }
    }