Limit Cart to Single Vendor

Sometimes admin wants the customer to purchase products from one vendor at a time. What they can do in this case is to add this code in the function.php of the theme :

add_action('woocommerce_add_to_cart_validation','woocommerce_add_to_cart_validation',10,3);
function woocommerce_add_to_cart_validation($passed, $product_id, $quantity){
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
        $cart_vendor = get_mvx_product_vendors($cart_item['product_id']);
        $product_vendor = get_mvx_product_vendors($product_id);
        if($cart_vendor && $product_vendor){
            if($cart_vendor->id != $product_vendor->id){
                $passed = false;
                wc_add_notice( __( 'Another vendor product is already in your cart.', 'woocommerce' ), 'error' );
                return $passed;
            }
        }
    }
    return $passed;
}

Add this and we assure you that you can limit customers to purchasing products from one vendor at a time.

Leave a Reply