Woocommerce html product description or short description shortcode with product ID

Viewed 2087

I want display same product description from another woocommerce product. So create a shortcode that shortcode get description by product

function get_product_Des( $atts, $content = null )
{
    extract(shortcode_atts(array(
        'product_id' => ''
       ), $atts));
    
    $ReturnValue    = "";
    $product        = wc_get_product( $product_id );
    if($product) $ReturnValue = $product->get_description('edit');
    return($ReturnValue);
}


add_shortcode('getproductdes', 'get_product_Des');

But product is that shortcode not show same as original main description. shortcode show only text, it remove

and some html code, is there anyway i can get as same description with same html code.

1 Answers

Try the following revisited Shortcode code to display the html formatted product description (or the product short description) for a defined product ID:

add_shortcode('product_descr', 'display_product_description');
function display_product_description( $atts )
{
    extract(shortcode_atts( array(
        'post_id' => '',
        'short'   => '0' // '1' for short description
    ), $atts));

    $content = $short == 0 ? get_the_content(null, false, $post_id) : get_the_excerpt( $post_id );

    return wpautop($content);
}

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

EXAMPLE USAGE:

  • For product description: [product_descr post_id="37"]
  • For product short description: [product_descr post_id="37" short="1"]
Related