Is there a shortcode for product description in WooCommerce

Trooller picture Trooller · Nov 13, 2018 · Viewed 9.5k times · Source

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.

Answer

LoicTheAztec picture LoicTheAztec · Nov 13, 2018

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']" );