简体   繁体   中英

Allow shortcodes in product variations description in Woocommerce

I would like to use a pods shortcode field inside the woocommerce variable product description but by default the variable description field does not support shortcodes.

The variation description is stored in an array woocommerce_available_variation , so I can't simple call the function do_shortcode($variation).

I am trying to allow short codes in this field by using the below code:

add_filter( 'woocommerce_available_variation', 'shortcode_variation_description');

function shortcode_variation_description( $variation ) {
$variation['variation_description'] = do_shortcode( $variation['variation_description'] );
return $variation;

But it's not working.

Anybody to figure out what I am doing wrong?

When using your code, it works. To test, I have used the Woocommerce shortcode [products] in a variation description as follow:

The imputed text is (where 37 is a real simple product ID):
"This is a description with a shortcode… [products ids="37"] As you can see this shorcode is detected and displayed."

And I get this display:

在此处输入图片说明

So it works for real. I have lightly made some little changes to this code version (yours work too) :

add_filter( 'woocommerce_available_variation', 'variation_description_allow_shortcodes', 10, 3 );
function variation_description_allow_shortcodes( $variation_data, $product, $variation ) {
    $variation_data['variation_description'] = do_shortcode( $variation_data['variation_description'] );

    return $variation_data;
}

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

You can define your own shortcode, see the documentation. You can copy/paste the code because it's wrapped in a class so it won't trigger fatal error due to double declaration.

But shortcode do not work that way :

function shortcode_handler($atts) {
  //code goes here
 }
add_shortcode("name_of_shortcode","shortcode_handler');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM