Conditionally remove variations from frontend
-
Hi,
I have three attributes, and I would like to conditionally hide one or two from the front end depending on site lang.
Attributes are, size(cm), color, size(in).
A single product page will either display size(cm) and color, or size(in) alone.
In the back-end, here is a short version of what my variations look like:
Size(cm) Color Size(in)
1- [50] [red] [any]
2- [any] [any] [20]I use this function to unset the attribute that I don’t need
if ( sitelang === 'fr' ){ add_filter( 'woocommerce_product_get_attributes', 'my_attribute_hider' ); function my_attribute_hider ( $attributes ) { if ( isset( $attributes["pa_size-in"] ) ){ unset( $attributes["pa_size-in"] ); } }
if ( sitelang === 'en' ){ add_filter( 'woocommerce_product_get_attributes', 'my_attribute_hider' ); function my_attribute_hider ( $attributes ) { if ( isset( $attributes["pa_color"] ) ){ unset( $attributes["pa_color"] ); } if ( isset( $attributes["pa_size-cm"] ) ){ unset( $attributes["pa_size-cm"] ); } }
Price of variation #1 will always override price of #2 .. so on single product pages where only the attribute Size(in) is available, price will be wrong .. If I re-arrange the variations order now price of pages that have the attributes Size(cm) and Color will be wrong.
Any idea how one would achieve this ?
I tried this filter with no luck:
add_filter('woocommerce_available_variation', 'remove_variation'); function remove_variation( $variation ){ if ( !empty($variation['attributes']['attribute_pa_color']) ){ return $variation; } else{ return ''; } }
- The topic ‘Conditionally remove variations from frontend’ is closed to new replies.