Skip Navigation

[Resolved] WooCommerce Attribute Conditional Shortcode

This support ticket is created 5 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 3 voices.

Last updated by jean-francoisB 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1203195

Lee

Hi, i'm trying to use the attribute shortcode (wpv-woo-list_attributes) to create a conditional output. I want to say:

If the attribute 'colour' = 'grey' then show option A
If the attribute 'colour' = 'blue' then show option B

And so on, how do I build the shortcode, there is only (wpv-woo-list_attributes) listen on the shortcodes page and no example.

Thanks
James

#1203730

Hi James,

Thank you for contacting us and I'll be happy to assist.

The "wpv-woo-list_attributes" shortcode generates a table of available attributes, so it can't be used for a such a conditional check.

If you'd like to build a conditional check to see if a specific attribute term is attached to a product, you can use "has_term" function ( https://codex.wordpress.org/Function_Reference/has_term ), as shown in this example:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/#specific

After you've added the "has_term" at "WP Admin -> Toolset -> Settings -> Front-end Content -> Functions inside conditional evaluations", you can use the conditional shortcode like this:


[wpv-conditional if="( has_term('Red', 'pa_color', null) eq '1' )" ]
 <p>This one is available in red!</p>
[/wpv-conditional]

Note: You'll replace "color" with actual attribute slug used on your website and keeping the "pa_" at the start.

I hope this helps and let me know how it goes.

regards,
Waqar

#1204407

Lee

My issue is resolved now. Thank you!

#1211650

Hi Waqar,

This is also something I wanted to reproduce on my website. But instead of a specific color condition, I would like to have a condition that do "If any color is selected, show this section" - "If there's no color selected, don't show this section". Because for now, if there's no specific color selected in the attributes, I have a blank spot on my product page where we can see "Color : ". I would like the full colour section to not show if there's no color selected in the attributes. What should I modify in your actual code to make this work like this?

Thank you!
J-F