Is there a shortcode for product description in WooCommerce

18,501

Solution 1

I wrote a very similar solution to @LoicTheAztec's answer above, but slightly more defensively (as his solution was breaking Elementor edits for me due to there not being a product context when it executes the shortcode on save).

It also solves your paragraph/newline issue as the content is formatted (basically replacing newlines with <p> tags) before returning.

function custom_product_description($atts){
    global $product;

    try {
        if( is_a($product, 'WC_Product') ) {
            return wc_format_content( $product->get_description("shortcode") );
        }

        return "Product description shortcode run outside of product context";
    } catch (Exception $e) {
        return "Product description shortcode encountered an exception";
    }
}
add_shortcode( 'custom_product_description', 'custom_product_description' );

Solution 2

You can build your own shortcode this way:

add_shortcode( 'product_description', 'display_product_description' );
function display_product_description( $atts ){
    $atts = shortcode_atts( array(
        'id' => get_the_id(),
    ), $atts, 'product_description' );

    global $product;

    if ( ! is_a( $product, 'WC_Product') )
        $product = wc_get_product($atts['id']);

    return $product->get_description();
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

Examples of USAGE [product_description]

1) In the current product page ph:

echo do_shortcode( "[product_description]" );

2) In any php code providing the related product ID

echo do_shortcode( "[product_description id='37']" );
Share:
18,501
Trooller
Author by

Trooller

Updated on June 07, 2022

Comments

  • Trooller
    Trooller almost 2 years

    Is there any shortcode to call the product description(text field under the title)?

    For now, I'm using another custom field to do this job but it will be better if I use the WooCommerce field.

  • Trooller
    Trooller over 5 years
    Worked! But the text is no more recognizing 'enter' as a paragraph or break line, just if I use <p> or <br>. Also not recognizing woocomerce shortcode like that "[products class="suggestion" ids="686"]"(this is woocomerce default field..my other custom field is still working)
  • LoicTheAztec
    LoicTheAztec over 5 years
    @Trooller Just inspect WordPress the_content() and get_the_content() available source code, to add in this shortcode function everything needed to get what you want.
  • Trooller
    Trooller almost 5 years
    Worked perfectly! Did you encounter any problems during long term use? I intend to apply the change in all my posts. I used an ACF to make my content.