Skip Navigation

[Resolved] Conditional output does not work with custom shortcode

This support ticket is created 8 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by Dejun Jing 8 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#340273

This issue is related with my previously closed post

https://toolset.com/forums/topic/populate-woocommerce-product-prices-to-custom-fields/

I am not sure whether I will get respond if I continue to post in that post, so I create a new post here.

I defined following short code for extracting woocommerce product regular and sale prices.

function get_regular_price($atts) {
    $price = money_format('%.2n', get_post_meta( get_the_ID(), '_regular_price', true));
    return $price;
}
add_shortcode("regularprice", "get_regular_price");


function get_sale_price($atts) {
    $price = money_format('%.2n', get_post_meta( get_the_ID(), '_sale_price', true));
    return $price;
}
add_shortcode("saleprice", "get_sale_price");

Both the shortcodes '[regularprice]' and '[saleprice]' works great.

I already registered these two custom shortcodes in Views > Settings > Compatibility > 3rd Party Shortcodes.

Now I am trying to use conditional view output to handle the no-discount case, i.e. only regular price, no sale price.

I created a product with only regular price but no sale price. Then I add following conditional out:

[wpv-conditional if="( '[saleprice]' eq '' )"]
No sale 
[/wpv-conditional]

If I put this wpv-conditional block inside a custom WYSIWYG field, it not only does not show anything, but also completely wipe out all existing content in the field. If I put it in a post body, it does not wipe out existing contents, but does not show anything.

Even though I put a sale price for the product, the wpv-conditional block still have same issue.

BTW, I feel your editor has an issue. When I click 'conditional output' button, some part of the popup window is blocked by the editor buttons, maybe meaning that your CSS z-index is not correctly set.

#340277

Well, you check in your Code if the regular_price is '' (means, nothing, not even '0')

What you should check instead, if you want to display something ONLY if the product has NO sales price, you could eventually do:

[wpv-conditional if="( empty($(_sale_price)) )"]This field is empty or does not exist[/wpv-conditional]

Please let me know if the above solution works for you, I look forward to your reply!

Regarding the CSS issue, do you mean in the Post Editor, Text Mode, or in another Editor?
Could you perhaps open a new ticket for this, in case you can reproduce it with Updated Plugins (if necessary) and no 3rd Party Plugins?

It works fine on my end.

Thank you for your patience.

#340281

Yes, I should have try empty() function....I will report back after another try. Thanks a lot.

#340286

Sure, no problem.

Please don't hesitate to inform me in case the issue persists and let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#340316

Your code of using 'empty($(_sale_price))' works perfectly!!!

I am still curious about why using custom shortcode does not work.

[wpv-conditional if="( '[saleprice]' eq '0' )"]
No sale
[/wpv-conditional]

It does not matter what I use for checking logic, 0 or nothing....It seems that as long as I use wpv-conditional with custom shortcode, it does not work. It goes beyond not working, but wipe out all contents of the WYSIWYG text field!

Anyway, I already get a great solution for my problem. If you think it is not worthy to investigate why wpv-conditional + custom shortcode does not work, I can close this thread.

Your great support is sincerely appreciated!

Derek

#340318

The correct approach is to check for empty field, as empty is ≠ to "0" or "".

However, the shortcode evaluation as in this exemple:

[wpv-conditional if="( '[wpv-archive-title]' ne '0' )"][/wpv-conditional]

should work.

Note that, aaboveble I check if the archive title is NOT = "0"

Same in your code, if you want to check if the sales ShortCode dos NOT return "0" you should use "ne" as operator.

Also you must see, you return a $price in your shortcode that is formatted.
It's not just a value as "0" or "1" but a money_format('%.2n') etc.

That can influence the comparison, although not wipe out not-in-conditional included content.

If that is a reproducible issue, please could you file it in a separate Ticket as we should then certainly analyze it?

Is it also happening with "default" Views ShortCodes, everywhere or only in WYSIWYG / Post Body's?

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#340403

The problem in this thread is solved, so I close the thread.

As to the remaining possible minor issue, I might open a new ticket later if necessary.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.