Add the below code in the functions.php file of your active theme to limit cart to single vendor
/**
* Restrict the WooCommerce cart to products from one store.
*
* Store meta key: multivendorx_store_id
* Variable products use their parent product's store ID.
*/
add_filter(
'woocommerce_add_to_cart_validation',
'restrict_cart_to_one_store',
10,
5
);
function restrict_cart_to_one_store(
$passed,
$product_id,
$quantity,
$variation_id = 0,
$variations = array()
) {
if ( ! $passed ) {
return false;
}
// Resolve the product ID to its parent if it is a variation.
$new_product_id = $variation_id
? wp_get_post_parent_id( $variation_id )
: $product_id;
if ( ! $new_product_id ) {
$new_product_id = $product_id;
}
// Get the store ID of the new product.
$new_store_id = get_post_meta(
$new_product_id,
'multivendorx_store_id',
true
);
// Allow products without an assigned store ID.
if ( $new_store_id === '' ) {
return $passed;
}
// Check existing cart items.
if ( ! WC()->cart || WC()->cart->is_empty() ) {
return $passed;
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
$existing_product_id = $cart_item['product_id'];
// Get the existing cart product.
$existing_product = wc_get_product(
$existing_product_id
);
if ( ! $existing_product ) {
continue;
}
// Resolve variations to their parent product.
$existing_parent_id = $existing_product->is_type( 'variation' )
? $existing_product->get_parent_id()
: $existing_product_id;
// Get the existing product's store ID.
$existing_store_id = get_post_meta(
$existing_parent_id,
'multivendorx_store_id',
true
);
// Prevent adding products from a different store.
if (
$existing_store_id !== '' &&
(string) $existing_store_id !== (string) $new_store_id
) {
wc_add_notice(
'You can only purchase products from one store at a time. Please remove the existing store products from your cart before shopping at another store.',
'error'
);
return false;
}
}
return $passed;
}




