1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. Limit checkout to a single vendor

Limit checkout to a single vendor

There might be a time when you want to run a marketplace but only allow the customer to purchase from a single vendor at a time. We do no support multiple cart checkout, but you can limit the checkout to a single vendor’s products. The following snippet will allow this. 

Add the following code to your themes functions.php

// Limit checkout to a single vendor store 
add_action( 'woocommerce_add_to_cart_validation', 'limit_single_vendor_to_cart', 10, 3 );
function limit_single_vendor_to_cart( $valid, $product_id, $quantity ) {

	$vendor_id = get_post_field( 'post_author', $product_id );

	// loop through the cart to check each vendor id
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		$cart_product_id 	= $cart_item[ 'product_id' ];
		$cart_vendor_id 	= get_post_field( 'post_author', $cart_product_id );
		if( $cart_vendor_id != $vendor_id ) {
			$valid = false;
			break;
		}
	}

	if ( ! $valid ){
		// Display a message in the cart. 
		wc_clear_notices();
		wc_add_notice( __( "Cart is limited to products from a single store at a time. Please checkout before purchasing from another store."), 'error' );
	}
	
	return $valid;
}

actions

Updated on February 26, 2021

Was this article helpful?