Search code examples
phpwordpresswoocommerceproductcart

Woocommerce: How to allow only one product type in a cart?


I have two product types, one of them simple product $cart_item['default-engraving'] and the other one credit product $cart_item['default-engraving'] && $cart_item['iconic-engraving']. I'm trying to find a solution how to make that if I add simple product to a cart, credit product shouldn't be added. Or if I add credit product to a cart, simple product should't be added. But if I want I could add same type for example simple product type as many as I want.


Solution

  • Update: Is not possible to detect custom cart item data on add to cart event.

    Checking cart items will allow you to prevent having cart items that have $cart_item['default-engraving'] and $cart_item['iconic-engraving'] at the same time:

    add_action( 'woocommerce_check_cart_items', 'check_cart_items_custom_data' );
    function check_cart_items_custom_data() {
        // Initializing: set the current product type in an array
        $types = [];
    
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $item ){
            if( isset( $item['default-engraving'] ) )
                $types[] = 'default';
    
            if( isset( $item['iconic-engraving'] ) )
                $types[] = 'iconic';
        }
    
        $types = array_unique( $types );
    
        // Check the number of product types allowing only one
        if( count( $types ) > 1 ){
    
            // Displaying a custom notice and avoid checkout
            wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.


    Original answer: (it can't work for your case as it's not possible to detect that on add to cart event)

    Here is the way targeting the product type to allow only one in cart:

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
    function only_one_product_type_allowed( $passed, $product_id, $quantity ) {
    
        // Initializing: set the current product type in an array
        $types = [ wc_get_product( $product_id )->get_type() ];
    
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $item ){
            // Set each product type in the array
            $types[] = wc_get_product( $item['product_id'] )->get_type();
        }
    
        $types = array_unique( $types );
    
        // Check the number of product types allowing only one
        if( count( $types ) > 1 ){
    
            // Displaying a custom notice
            wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
            return false; // Avoid add to cart
        }
    
        return $passed;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Related: Allow only one product category in cart at once in Woocommerce