Show first available variant if the combination is unavailable

Hey all,

Let's say we have a product with

Size: small, medium, large

colour: red, blue, yellow

and yellow is not available in small. Now if someone has chosen small and then chooses yellow, I want my shop to show medium/yellow instead (something like Amazon).

Not looking for a full solution here just some advice is much appreciated

thank you

-Like and Mark as an Accepted Solution if my reply is helpful
I'm not an expert, these are my own experiences or codes that I gathered from others. I'm using Debut.
I have my own online pet supply store check it out at