I am trying to: To filter a WooCommerce attribute on a certain value, in this case: ingress-protection and put the result in a shortcode.
Only when the value returned is 'IP65 from Optical side', 'IP40 from Optical side' or 'IP44 from Optical side', the value should be displayed. Like in the example below for IP65. I'm not sure how to create a function out of it en put in a shortcode so I could use it some other elements in my theme.
[wpv-conditional if="( $(attribute_pa_ingress-protection) eq 'IP65 from Optical side' )"]
IP65
[/wpv-conditional]
Thanks.
Hello,
There isn't such a built-in feature within Views plugin, you might consider custom codes, for example, create a custom PHP function to check if the product post has specific "attribute" value, then put this function insider the wpv-conditional shortcode for evaluation.
More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
For the custom PHP codes, I have searched it in google, and found some related posts, for example:
https://stackoverflow.com/questions/49712333/how-to-check-if-product-has-a-specific-product-attribute-in-woocommerce
For your reference.
Hi Luo,
I figured out that I don't need a shortcode and can just filter a field. I tried this:
[wpv-conditional if="( $(attribute_pa_ingress-protection) eq 'IP65 from Optical side' )"]
IP65 from Optical side
[/wpv-conditional]
But it isn't working or showing IP65 from Optical side? It just shows nothing. The value is available in this attribute and added for this specific product.
[wpv-post-taxonomy type="pa_ingress-protection"] returns "IP65 from Optical side" on the product page, so this proofs the attribute is not empty er so.
The argument "$(attribute_pa_ingress-protection)" works only for custom post field.
Since you can get the value from shortcode: [wpv-post-taxonomy type="pa_ingress-protection"], then you can use the shortcode into the [wpv-conditional] for example:
[wpv-conditional if="( '[wpv-post-taxonomy type="pa_ingress-protection" format="name"]' eq 'IP65 from Optical side' )" ]
IP65
[/wpv-conditional]
More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
Using shortcodes in conditions
Hi,
Thanks a lot. wpv-post option also works for products? In this case I'm using the conditional statement on a product page, but it isn't working or showing anything at this point? The attribute (Ingress-protection) and value mentioned do exist.
Please try this:
1) Create a custom function "has_woo_attr_term", add below codes in your theme file "functions.php":
function has_woo_attr_term($term, $tax){
$res = '0';
if(has_term($term, $tax, null)){
$res = '1';
}
return $res;
}
2) Dashboard-> Toolset-> Settings-> Front-end Content
in section "Functions inside conditional evaluations", add the function name: has_woo_attr_term
3) Use below codes in your content:
[wpv-conditional if="( has_woo_attr_term('IP65 from Optical side', 'pa_ingress-protection', null) eq '1')"]
IP65
[/wpv-conditional]
And test again
Thanks a lot, that worked!:) Where can I find the required documentation so I can replicate this myself?
My issue is resolved now. Thank you!