Skip to content

Instantly share code, notes, and snippets.

@digitalchild
Created November 19, 2020 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalchild/ddbd93fad833335ec986b9b35e56d35d to your computer and use it in GitHub Desktop.
Save digitalchild/ddbd93fad833335ec986b9b35e56d35d to your computer and use it in GitHub Desktop.
Limit cart and checkout to a single vendor stores products in WC Vendors Marketplace and WC Vendors Pro.
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment