• Resolved redoan99

    (@redoan99)


    Hello, is there any system to block customers from adding to cart different types of product (e.g. Virtual, Physical, Downloadable) at the same time. Or is there any system to block customers from checkout of different types of product at the same time.
    My problem is I am selling both virtual and physical product at my store. And disabled COD for virtual product from woocommerce settings. And it works when there is only virtual product in the cart. How can I get rid of this problem?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Roxy

    (@roxannestoltz)

    Hi @redoan99 ,

    I understand that you would like to prevent Virtual and Physical products from being combined in the cart.

    You can try the following code snippet to see if this works for you:

    add_filter( 'woocommerce_add_to_cart_validation', 'filter_wc_add_to_cart_validation', 10, 3 );
    function filter_wc_add_to_cart_validation( $passed, $product_id, $quantity ) {
        $is_virtual = $is_physical = false;
        $product = wc_get_product( $product_id );
    
        if( $product->is_virtual() ) {
            $is_virtual = true;
        } else {
            $is_physical = true;
        }
    
        // Loop though cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check for specific product categories
            if ( ( $cart_item['data']->is_virtual() && $is_physical )
            || ( ! $cart_item['data']->is_virtual() && $is_virtual ) ) {
                wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
                return false;
            }
        }
    
        return $passed;
    }
    
    // For security: check cart items and avoid checkout
    add_action( 'woocommerce_check_cart_items', 'filter_wc_check_cart_items' );
    function filter_wc_check_cart_items() {
        $cart = WC()->cart;
        $cart_items = $cart->get_cart();
        $has_virtual = $has_physical = false;
    
        // Loop though cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check for specific product categories
            if ( $cart_item['data']->is_virtual() ) {
                $has_virtual = true;
            } else {
                $has_physical = true;
            }
        }
    
        if ( $has_virtual && $has_physical ) {
            // Display an error notice (and avoid checkout)
            wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
        }
    }

    You need to add the code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the?Code snippets?plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

    Hope this helps ??

    Hi @redoan99

    I found this article that discusses things to consider when selling both physical and digital goods, which you may find helpful: https://woocommerce.com/posts/sell-digital-with-woocommerce/

    Just to mention – with reference to the snippet shared above, if need be, you can read more at this Stack Overflow thread and also this one.

    Hi Roxanne @redoan99, I’m having a similar problem at a store I’m working on right now. I have product category “X” when it is in the shopping cart and another product from a different category is added, it cannot be added and shows a message: “This product cannot be added…etc” and that when it has a product from some other category in the cart and I want to add the category “X” can not and shows a similar message. Thanks for your help!

    • This reply was modified 2 years, 2 months ago by ensedese.
    Saif

    (@babylon1999)

    Hello @ensedese,

    I understand you have a similar problem but we need to handle each request individually in forums.

    Please open your own thread and make sure to include as much information as you can.

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Prevent different kind of product from adding to cart’ is closed to new replies.