skip to Main Content

We need to add shortcode into the WooCommerce Product Description. The intent is that we can call blocks into specific products and just update that block vs. the each individual listing. Any idea how to do so? Currently it just shows the actual shortcode, not the content.

This is the shortcode: [block id="product-callouts-01"]

I tried this code in my Functions.php file, but to no avail:

if (!function_exists('woocommerce_template_single_excerpt')) {
   function woocommerce_template_single_excerpt( $post ) {
       global $post;
       if ($post->post_excerpt) echo '<div itemprop="description">' . do_shortcode(wpautop(wptexturize($post->post_excerpt))) . '</div>';
   }
}

2

Answers


  1. Try This code to your function.php

    function custom_woocommerce_product_description($description) {
    
        $pattern = '/[block id="([^]]+)"]/'; 
        if (preg_match($pattern, $description, $matches)) {
            $shortcode = $matches[0];
            $block_id = $matches[1];
    
            $block_content = get_block_content_by_id($block_id); 
            $description = str_replace($shortcode, $block_content, $description);
        }
    
        return do_shortcode($description);
    }
    
    add_filter('woocommerce_short_description', 'custom_woocommerce_product_description');
    
    function get_block_content_by_id($block_id) {
        $block_content = "Replace this with the actual content of the block";
    
        return $block_content;
    }
    
    ?>
    
    Login or Signup to reply.
  2. To add a shortcode to the product description, you can try the following:

    add_filter( 'the_content', 'change_product_description', 10, 2 );
    function change_product_description( $post_content, $post_id ) {
        if( get_post_type($post_id) === 'product' ) {
            $post_content .= do_shortcode('[block id="product-callouts-01"]');
        }
        return $post_content;
    }
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search