1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

4

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • The value is OK, but the label is not being fetched. Edit: apparently pa_ is important. What does that do? – Johan Oct 18 '22 at 15:26