-1

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>';
   }
}
3
  • 1
    You should provide your code in your question, even if it doesn't work. Commented Sep 18, 2023 at 17:32
  • Is it for product description or for product short description (excerpt)? Commented Sep 18, 2023 at 17:45
  • Product description (main one) Commented Sep 18, 2023 at 17:47

2 Answers 2

0

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;
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

When you say "replace this with actual content of the block", are you referring to the text in the block or the shortcode? We don't want to be having to edit the functions file each time when making changes to the block.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

To add a shortcode to the product description, you can try the following:

add_filter( 'the_content', 'change_product_description' );
function change_product_description( $post_content ) {
    global $post;

    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.

2 Comments

This is bringing back the following error: "Uncaught ArgumentCountError: Too few arguments to function change_product_description()"
@IVCatalina Sorry I have updated my answer code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.