Feu2057
Feu2057

Reputation: 1

How to force remove products from WooCommerce cart?

I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.

Upvotes: 0

Views: 1849

Answers (1)

Suman Biswas
Suman Biswas

Reputation: 883

You can use this code below :

add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
    global $woocommerce;
    $cartItem = $woocommerce->cart->cart_contents;
    $currentProductId = $q->id;
    foreach($cartItem as $item)
    {
        $productItemId = $item['product_id'];
        ///your condition will be here

    }
    return $q;
}

You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.

Upvotes: 1

Related Questions