Something like this?

ifdown $WAN2 # Shutdown WAN2 ifup $WAN1 # Enable WAN1
Or am I thinking to simple?

If there is a Wifi connection, no 3G/4G is needed (ifdown 3/4G)… As soon as Wifi connection is down, connect 3/4G (ifup 3/4G)… If wifi is back again, ifdown 3/4G… ??