wordpresswoocommercewoocommerce-theming

wordpress/woocomerce get variation_description withouth html escaping


I have some products with variations and want to provide some data in the description as JSON for working with it in the template.

In the product, I can see the data is correct, for example:

{"next_date":"2021-05-12","days":"-1217","tmp1":"1","tmp2":"149.53400000000002","tmp3"

The Data is also saved clear in the db in the meta field _variation_description

I loop now all variations (All JSON are only samples)

foreach ( $product->get_available_variations() as $variation_data ) {
            print_r( $variation_data['variation_description']);

And I get

 <p>{&#8222;next_date&#8220;:&#8220;2019-03-11&#8243;,&#8220;days&#8220;:&#8220;-2009&#8243;,&#8220;tmp1&#8243;:&#8220;1&#8243;,&#8220

I can now use strip_tags and use HTML entity decode, but then I have the problem

{„next_date“....

In the $variation_data I have the variation ID, so I can get the data over get_meta, but this seems is not the best thing I should use because it's another sql query I don't need.

So is here any way to get the old unescaped JSON in this field?

Thanks


Solution

  • You can probably try this:

    change the default array output to object output:

    foreach ( $product->get_available_variations('objects') as $variation ) {
                print_r( $variation->get_description());
    }
    

    This will give you an object of variations and then from that object you can get the description using the get_description() method which will not execute another query as you mentioned.