0
votes

I have a woocommerce store with two categories T-Shirts and Jeans with various colours and sizes.

For the t-shirts category I've currently setup the Variables as "Colour + Any Size"

What I would like to hide certain colour options depending on the selected size.

So for example : A T-Shirt has variable : Sizes : Small | Medium | Large Colours : Black | Red | Blue

What I want to do is write some code that will disable the colours 'Black' and 'Blue' if the user selects the size : 'Large'

My knowledge of PHP is limited but I have managed to learn some code by posting questions and patching together snippets. So far I have the following snippets of code (which i'm certain is a mess). Any help in putting it together correctly to form some coherent block of working code would be greatly appreciated.

global $woocommerce, $product, $post;
foreach ( $attributes as $attribute_name => $options )

// check if product belongs to T-shirt Category
if ( is_product() && has_term( 't-shirts', 'product_cat' )  

//check if product is variable
{ if ($product->is_type( 'variable' )) 
{
    $available_variations = $product->get_available_variations();
    foreach ($available_variations as $key => $value) 
    { 

    if ( $attribute_name == 'Size')
     {
       // check if selected value is Large 
       if ($value = 'Large'){ 
        unset($value['Black']); // unset that option from array
       } 
    }
}
1

1 Answers

0
votes

This code will help you if you entered products as variations.

return 50;
}
add_filter( 'woocommerce_ajax_variation_threshold', 'custom_wc_ajax_variation_threshold', 10, 2 );```