Reputation: 23
Background: I working on creation of marketplace based on woocommerce plugin. So since each seller have different conditions like shipping fee, minimum amount, etc I would like to allow customers add to cart only from one vendor per order.
Actions taken so far: I can compare cart items vendor with current vendor. It should be noted that code below is not properly working if W3 Total Cache enabled
function check_cart_by_vendor() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$_product = array();
foreach($items as $item => $values) {
$_product[] = $values['data']->post;
}
if(isset($_product[0]->ID)){
$product_in_cart_vendor_id = get_post_field( 'post_author', $_product[0]->ID );
global $post;
$vendor_id = get_the_author_meta('ID');
if ($vendor_id == $product_in_cart_vendor_id){echo "same vendor";}else{echo "another vendor";}
}else{echo "same vendor";}
}
What I want scenario: Customer added to cart products from one vendor then he select another vendor and again added to cart product from another vendor. When he added products from 2nd vendor I want his cart automatically cleared from 1st vendor`s products
Question: How to implement above scenaio?
Upvotes: 1
Views: 1159
Reputation: 1
the code does not work to me, dont let me add any item to my cart, when i put add to cart, the cart back to 0 items. I found the modification of your code on this site: https://www.wcvendors.com/help/topic/restrict-clientbuyer-to-order-from-one-vendor-at-a-time/
Its works perfect on my Dokan multivendor project. They also add a message to the user.
i will put the code below (modified), i use a code snnipet plugin to add the function:
add_action( 'woocommerce_add_to_cart_validation', function( $is_allow, $product_id, $quantity ) {
$product = get_post( $product_id );
$product_author = $product->post_author;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_product_id = $cart_item['product_id'];
$cart_product = get_post( $cart_product_id );
$cart_product_author = $cart_product->post_author;
if( $cart_product_author != $product_author ) {
$is_allow = false;
break;
}
}
if( !$is_allow ){
// We display an error message
wc_clear_notices();
wc_add_notice( __( "No puedes comprar productos en diferentes sucursales. Revisa en que tienda te encuentras para seguir comprando.", "wcfm-ecogear" ), 'error' );
}
return $is_allow;
}, 50, 3 );
Upvotes: 0
Reputation: 23
I figured it out by myself. Below is the code
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
$prodId = (int)$_POST["add-to-cart"];
$product_in_cart_vendor_id = get_post_field( 'post_author', $_product[0]->ID);
$product_added_vendor_id = get_post_field( 'post_author', $prodId );
if( $product_in_cart_vendor_id !== $product_added_vendor_id ){$woocommerce->cart->empty_cart();}
return $cart_item_data; }}
Upvotes: 1