Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

Get specific product attribute and display it in Woocommerce Single Product Pages


I have created an attribute called 'dimensions'

I wish to add a snippet that will :-

Display the dimensions attribute (Label and Value) in the single product page under meta.


Solution

  • The code below will display under meta section, the product attribute 'dimensions' label and value in single product pages:

    add_action( 'woocommerce_single_product_summary', 'product_attribute_dimensions', 45 );
    function product_attribute_dimensions(){
        global $product;
    
        $taxonomy = 'pa_dimensions';
        $value = $product->get_attribute( $taxonomy );
    
        if ( $value ) {
            $label = get_taxonomy( $taxonomy )->labels->singular_name;
    
            echo '<p>' . $label . ': ' . $value . '</p>';
        }
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and work.