Search code examples
phpwoocommerceproductcustom-taxonomytaxonomy-terms

Display specific product attribute description in WooCommerce single product page


I have attribute "Details" and one of the values of this attribute - "Oversize". This value have description "Please note, this model runs large. We recommend ordering a size smaller".

So, I want to get this attribute's value name and description for product and then show it on single product page.

I use:

add_action( 'woocommerce_single_product_summary', 'my_short_desc', 16 );

function my_short_desc(){
global $product;
$detail_name = $product->get_attribute('details');
//$detail_desc = how to get it?

echo $detail_name . '<br>' . $detail_desc;
}

I don't understand, how to get value's description.

I know, that there is function term_description(), but if I use term_description($detail_name) it's don't work. I need to use value's ID, but I don't know, how to get it fast in my function.


Solution

  • If it's a global product attribute (taxonomy), you can use the following to display specific product attribute term name and description, in single product page:

    add_action( 'woocommerce_single_product_summary', 'display_product_attribute_short_desc', 16 );
    function display_product_attribute_short_desc(){
        global $product;
    
        $taxonomy = 'pa_details'; // Product attribute taxonomy (always start with "pa_")
        $terms = get_the_terms($product->get_id(), $taxonomy); // Get product attributes terms
    
        if ( $terms ) {
            $term = current($terms); // Get the first term if many
    
            echo $term->name . ( $term->description ? '<br>' . $term->description : '');
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.