Skip Navigation

[Resolved] WooCommerce attribute filter

This thread is resolved. Here is a description of the problem and solution.

Problem:

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.

Solution:

It needs custom codes, see details here:

https://toolset.com/forums/topic/woocommerce-attribute-filter/#post-1156148

Relevant Documentation:

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

This support ticket is created 6 years 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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Luo Yang 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1154806

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.

#1155167

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.

#1155570

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.

#1156006

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

#1156077

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.

#1156148

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

#1156232

Thanks a lot, that worked!:) Where can I find the required documentation so I can replicate this myself?

#1156235
#1158773

My issue is resolved now. Thank you!

#1159178

You are welcome