0
votes

I have a woocommerce variable product with 2 sets of attributes. For simplicity's sake let's say it's a car.

2 attributes:
size : small|medium|large
colour: red|green|blue

Normally this would mean that I could select any car size in any colour.

However, the small and medium cars actually only come in red, green or blue,
BUT the large car comes in only gold or silver.

What I want to be able to have happen is:
if the user selects small or medium size, the red|green|blue options appear in the colour dropdown,
but if they select the large car, only gold & silver appear.

I have absolutely no idea where to start with this.

1
This is not already happening? Thought it was a WooCommerce 2.4 thing, but just test WC2.3.10 and unavailable colors were showing in the drop down after selecting a color. - helgatheviking
Update WooCommerce to the latest version. There were a lot of improvements made to WooCommerce 2.3 variations that are worth doing. There is even more with WooCommerce 2.4 that will be released very soon. - Michael

1 Answers

0
votes
function hide_out_of_products( $is_visible, $id ) {
$product = new wC_Product( $id );

    if (! $product->is_purchasable()) { //OR (! $product->is_in_stock()) If you want to check if in stock instead.
        $is_visible = false;
    }

    return $is_visible;
}
add_filter( 'woocommerce_product_is_visible', 'hide_out_of_products', 10, 2 );