Last active
August 19, 2018 20:14
-
-
Save marcbovet/89cfcd2bee451e15c9e0a10267f3d7cd to your computer and use it in GitHub Desktop.
Revisions
-
marcbovet revised this gist
Jun 30, 2017 . 1 changed file with 21 additions and 33 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,43 +1,31 @@ // 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; } } -
marcbovet created this gist
May 12, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ // Woocommerce // Restrict customer to buy in only one shop ! function woo_custom_add_to_cart_with_variations( $cart_item_data ) { //Compliant with product-variations //Tested with woocoommerce 3.0.5 | 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']->post; $_product[] = $values['data']; break; //after 1st product we can stop the loop } if(isset($product_id_in_cart)){ //GET FIRST PRODUCT IN CART if ($_product[0]->is_type('variable')){ //check if product is a variation $product_id_in_cart = $_product[0]['variation_id']; } //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 $prodId = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_GET['add-to-cart'] ); //get last item added to cart $product_added = wc_get_product( $productId ); //get product from ID if ($product_added['variation_id']){ //check if product is a variation $product_id_added= $product_added['variation_id']; } else //simple product { $product_id_added = $prodId; } //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 only order items from 1 shop !", "wcvendors"),'error');} return $cart_item_data; } }