Claim your freedom to choose the perfect partner for your multi-vendor journey.

FREEDOM25

Celebrate the festive season by giving your marketplace a powerful lift!

FESTIVE20

5.0.x
Multi-store. Franchise. Facilitator. AI tools.
Everything your marketplace needs - now in one platform.
Power your marketplace dreams with unbeatable Black Friday deals!

MVXBLACK30

Supercharge your marketplace vision with unstoppable Cyber Monday deals!

MVXCYBER30

Holiday cheer, bigger savings
Take 25% off-because your marketplace deserves a gift too.

happyholiday

-
DAYS
-
HOURS
-
MINUTES
-
SECONDS
20% Off Is Temporary. The Benefits Aren't.

HAPPY20

Join the MultiVendorX Facebook Community

Contact, share, and grow with thousands of MultiVendorX users around the world.
View Categories

Limit Cart to Single Vendor

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;
}

Leave a Reply

Shopping Cart
Launch Your Marketplace
in Days, Not Month
Get expert guidance to build, scale, and grow your MultiVendorX marketplace
Book Free Strategy Call
Trusted by 10000+ marketplace Owners
Scroll to Top