Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save itsfreddyrb/6d6d3054a1118a615370b6ff15d206be to your computer and use it in GitHub Desktop.
Save itsfreddyrb/6d6d3054a1118a615370b6ff15d206be to your computer and use it in GitHub Desktop.
WC_Vendors Woocommerce restrict buying from multiple vendors
add_action('woocommerce_add_to_cart', 'custom_add_to_cart');
function custom_add_to_cart() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$cart_products = array();
$remove_this_items = array();
//Get all the items in the cart
foreach($items as $item => $values) {
$cart_products[] = $values['product_id'];
}
$last_product_added_to_cart = end($cart_products);
$last_product_added_to_cart_vendor_id = WCV_Vendors::get_vendor_from_product( $last_product_added_to_cart );
for ($i=0; $i <= count($cart_products); $i++) {
if(WCV_Vendors::get_vendor_from_product($cart_products[$i]) > 0){
if(WCV_Vendors::get_vendor_from_product($cart_products[$i]) != $last_product_added_to_cart_vendor_id){
foreach ( $items as $cart_item_key => $cart_item ){
if($cart_item['product_id'] == $cart_products[$i]){
WC()->cart->remove_cart_item( $cart_item_key );
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment