Huynh
Huynh

Reputation: 392

Why WooCommerce product get_description does not get content generated by shortcode

I am reading the class-wc-structured-data, which is the key file for generating product schema in the WooCommerce plugin. I wonder why my content, generated by inserting shortcode (tab content) into the WooCommerce text editor (full description of a product), is unable to be regconized by this code:

'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) )

The full of it, extracting from the file, is:

$markup = array(
            '@type'       => 'Product',
            '@id'         => $permalink . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist.
            'name'        => $product->get_name(),
            'url'         => $permalink,
            'image'       => wp_get_attachment_url( $product->get_image_id() ),
            'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ),
        );

Is there any solution to make shortcode content, inserted in the WooCommerce text editor, to be displayed as the 'description' in the schema generated by WooCommerce?

Upvotes: 1

Views: 2401

Answers (1)

zod
zod

Reputation: 487

I had a similar problem, my goal was to move the description from the "tabs" section to a different hook. So, i disabled the "tabs" section, then i added a custom action to display just the description. Sadly the shortcodes were not rendering.

I found this hint on the Woocommerce documentation: https://docs.woocommerce.com/document/allow-shortcodes-in-product-excerpts/

The same do apply to the description, so i modified my code adding the functions as they described: do_shortcode(wpautop(wptexturize($product->get_description()))).

My final code is as following, i hope it could help.

// Remove all tabs
add_filter( 'woocommerce_product_tabs', 'zod_remove_product_tabs', 98 );
function zod_remove_product_tabs( $tabs ) {
    unset( $tabs['description'] );          // Remove the description tab
    unset( $tabs['reviews'] );          // Remove the reviews tab
    unset( $tabs['additional_information'] );   // Remove the additional information tab
    return $tabs;
}
// Add description in the hook after summary 
add_action( 'woocommerce_after_single_product_summary', 'zod_product_full_description' );
function zod_product_full_description () {
    if(is_product()) {
    $product = wc_get_product( get_the_ID() );
    $description = do_shortcode(wpautop(wptexturize($product->get_description())));
    echo $description;
    }
}

Upvotes: 1

Related Questions