• Resolved TwoGunRosie

    (@twogunrosie)


    Is it possible to only offer variation options based on products already in the cart?

    My website sells TVs. We want to offer optional installation. Installation fees are based on TV size. Small TVs cost $xx to install. Large TVs cost $zz to install. We don’t want the customer to choose the wrong installation fee.

    I set up a variable product for installation based on TV size.

    Is there a way to show only the variation that applies based on the size of TV they will purchase?

Viewing 1 replies (of 1 total)
  • Saif

    (@babylon1999)

    Hello @twogunrosie,

    If I understand you correctly, you want to hide certain product variations unless a certain product is in the cart.

    At the time being, there’s no plugin in our store for this as it’s a very specific scenario. Please note custom solutions are not within our scope of support.

    That said, I found the a similar question on StackOverflow.

    I’ve added the snippet to match what you’re trying to do, here’s the end result.

    add_filter( 'woocommerce_variation_is_visible', 'hide_specific_product_variation', 10, 4 );
    function hide_specific_product_variation( $is_visible, $variation_id, $variable_product, $variation ) {
        // Here define the variation(s) ID(s) to hide 
        $variations_ids_to_hide = array('481');
        // Here enter the product ID or IDs that needs to be in the cart otherwise the the variation will be hidden. If 1 of two products need to me in cart you can do 343 or 334;
        $product_id = 343;
    
        // All the magic happens here ??
        if( !in_array( $product_id, array_column( WC()->cart->get_cart(), 'product_id' ) )  && in_array($variation_id, $variations_ids_to_hide ) ) {
            return false;
        }
        return $is_visible;
    }

    You can find comments in the code, please replace the numbers with your variation and product ID.

    You can find the product ID here: https://d.pr/i/IWlvOF

    Variation ID here: https://d.pr/i/M87ZkY

    If you need more help, I highly recommend working with a WooExpert.

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Conditional variation based on cart content’ is closed to new replies.