Home / Admin / Single Vendor Purchase
Duplicate Snippet

Embed Snippet on Your Site

Single Vendor Purchase

Limits the customers purchase to a single vendor

Code Preview
php
<?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;
}

Comments

Add a Comment