// Woocommerce // Restrict customer to buy in only one shop add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart_with_variations' ); function woo_custom_add_to_cart_with_variations( $cart_item_data ) { //Compliant with product-variations //Tested with woocoommerce 3.1 | WC Vendors PRO 1.4.3 global $woocommerce; $items = $woocommerce->cart->get_cart(); //getting cart items $_product = array(); foreach($items as $item => $values) { $product_id_in_cart = $values['data']->id; $_product[] = $values['data']; break; //after 1st product we can stop the loop } if(isset($product_id_in_cart)){ //GET VENDOR ID OF FIRST PRODUCT IN CART $product_in_cart_vendor_id = get_post_field( 'post_author', $product_id_in_cart); //get vendor id from cart. //GET LAST PRODUCT ADDED TO CART $product_id_added = (int) apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'])); //GET VENDOR ID OF LAST PRODUCT ADDED IN CART $product_added_vendor_id = get_post_field( 'post_author', $product_id_added ); //CHECK IF BOTH VENDOR IDs ARE EQUAL //TODO : ask user if he wants to empty his cart. if( $product_in_cart_vendor_id !== $product_added_vendor_id ){$woocommerce->cart->empty_cart();wc_add_notice( __('You can order only from 1 restaurant!', 'swiss_takeaway'),'error');} return $cart_item_data; } }