user3100283
user3100283

Reputation:

How to remove an item from cart based on condition in Woocommerce

I am working on different restaurants in one website. There is different menus on different restaurants. But the condition is one person can order from maximum one restaurant. So if suppose he enters in KFC restaurant and orders an item then he decided not to take food from KFC and then goes to Macdonalds page.And orders there 6 items.

Now in cart we have 7 items in cart (One from KFC and another 6 From MADONALDS). But as the condition is, one person can only order from maximum 1 restaurant so I need to remove the menu which was added from KFC Restaurant.

To make this task easy, I have introduced restaurant_id parameter in woocommerce cart_item_data.So i have two things that made it easier-

(1) $current_restaurant_id - Current Restaurant id (on which page i/you are currently staying)
AND
(2) restaurant_id in cart_item_data;

It was almost done but problem appeared in last moment:

My codes are here:

global $woocommerce;
$items = $woocommerce->cart->get_cart();
$current_restaurant_id = get_the_ID();
foreach($items as $item => $values) { 
    if($current_restaurant_id !== $values['restaurant_id']){
        WC()->cart->remove_cart_item($values['product_id']);
    }else{
        $_product = $values['data']->post; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        $output = '<li class="clearfix">';
        $output .= '<span class="pull-left">'. $_product->post_title .' ('. $values['quantity'] . ') ' . $values['restaurant_id'] . '</span>';
        $output .= '<span class="pull-right text-spl-color">'. $price*$values['quantity'] .'</span>';
        $output .= '</li>';
        echo $output;
    }
}

Nothing is showing on cart, but the items are in cart. No items are being deleted.

Upvotes: 1

Views: 2271

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254373

Here is the correct code for WooCommerce cart object… To remove an item from cart you have to use the key, not the value… The only thing that I can't test is your "restaurant_id". All the other code is tested and fully functional.

Here is this code:

$current_restaurant_id = get_the_ID();
foreach(WC()->cart->get_cart() as $key => $item) {
    $item_id = $item['product_id']; // the product ID
    $variation_id = $item['variation_id']; // if is a variable product, this is going to be > 0
    $item_price = $item['data']->price; // the price of the product
    if($current_restaurant_id !== $item['restaurant_id'])
    {
        WC()->cart->remove_cart_item($key);
    }
    else
    {
        echo '<li class="clearfix">
        <span class="pull-left">'. $item['data']->post->post_title .' ('. $item['quantity'] . ') ' . $item['restaurant_id'] . '</span>
        <span class="pull-right text-spl-color">'. $item['line_total'] .'</span>
        </li>';
    }
}

After that, I don't know where you are using this code or if you are using it in a hooked function, so I can't help more than that…

Upvotes: 3

Related Questions