2
votes

I have a Woocommerce website with many variable products. These products have numerous colour and size options.

I'd like to limit which variations are displayed, based on which category the product is accessed through.

I thought about creating multiple variable products, but this would have implications for stock. I need a single variable product but multiple "views" of the product that limit which variations are shown.

Detecting the parent category is no problem. I can't work out how limit the number of variations that are displayed. I could prevent the purchase of certain variations using the Woocommerce filter woocommerce_available_variation, but I actually want to remove the options from the page all together.

I see references documentation to "invisible" and "disabled" variations, but I can't see how I might be able to harness these.

I found two filters woocommerce_variation_is_active and woocommerce_variation_is_visible which allow you to grey-out or hide certain information for a particular variation. This still doesn't allow me to prevent them from displaying all together.

1
How many variations are available? The solution is different depending on the number of available variations.user9372991
Specifically, is the problem that you are trying to solve the removal of invalid options from the select HTML element?user9372991

1 Answers

0
votes

My client has requested something similar, the work around I did was.

I had a woocommerce folder in my template to override only specific php files I need, I assume you can or already have added it.

Then file responsible for displaying the select field for variable is located at:

/woocommerce/single-product/add-to-cart/variable.php

So we see a loop that draws the selects for each attribute.

you would want your unwanted fields unset before it will be passed to the "wc_dropdown_variation_attribute_options" function that draws it.

so here is what you (roughly) need to do:

<?php foreach ( $attributes as $attribute_name => $options ) : 

// you want to target shirts category
if ( is_product() && has_term( 'shirts', 'product_cat' ) {
 // you don't want a whole attribute displayed
  if ( $attribute_name == 'Colour'){
    unset($attribute_name);
  }
}

// if you don't want few specific items then 
if ( $attribute_name == 'Sizes'){ // color or size
    $options_temp = $options;           

    foreach ( $options_temp as $key => $option )
     {
       if ($option = 'XXL'){ 
        unset($options[$key]); // unset that option from REAL array
       }
     }
}
?>

tailor code to your needs.