Created
December 8, 2018 18:05
-
-
Save zainaali/f79702d4a57debaf119ef51df2077628 to your computer and use it in GitHub Desktop.
Prevent customers from ordering from more than 1 Vendors in Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Order only from 1 shop | |
//https://www.wcvendors.com/help/topic/restrict-clientbuyer-to-order-from-one-vendor-at-a-time/ | |
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' ); | |
function woo_custom_add_to_cart( $cart_item_data ) { | |
global $woocommerce; | |
$items = $woocommerce->cart->get_cart(); //getting cart items | |
$_product = array(); | |
foreach($items as $item => $values) { | |
$_product[] = $values['data']->post; | |
} | |
if(isset($_product[0]->ID)){ //getting first item from cart | |
$product_in_cart_vendor_id = get_post_field( 'post_author', $_product[0]->ID); | |
$prodId = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_GET['add-to-cart'] ); | |
$product_added_vendor_id = get_post_field( 'post_author', $prodId ); | |
if( $product_in_cart_vendor_id !== $product_added_vendor_id ){$woocommerce->cart->empty_cart();wc_add_notice( __("You can only order items from 1 Instructor !", "wcvendors"));} | |
return $cart_item_data; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment