简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM