22

Made a function where the customer get a product added to the cart when they reach a specific amount.

Example of when customer reaches level 3 and get the product added.

// Bonus products
$product_1 = '4751'; 
$product_2 = '4752'; 
$product_3 = '4753'; 

// Get cart value in a clean format
$cart_total = WC()->cart->get_cart_subtotal();
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);
$sum_raw = $cart_value;

// Set the sum level 
$level3 = '1500';

// Check sum and apply product
if ($sum_raw >= $level3) {

// Cycle through each product in the cart and check for match
$found = 'false';
foreach (WC()->cart->cart_contents as $item) {
    global $product;
    $product_id = $item['variation_id'];

    if ($product_id == $product_3) {
        $found = 'true';
    }
}

// If product found we do nothing 
if ($found == 'true') {}
// else we will add it
else {
    //We add the product
    WC()->cart->add_to_cart($product_3);

If customer decides to remove item's so this statement is true i want to be able to remove it again.

if ($sum_raw < $level3) {

    // Trying to remove item
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        if ($cart_item['variation_id'] == $product_3) {

            //remove single product
            WC()->cart->remove_cart_item($product_3);
        }
    }
}

Am do not manage to remove the product from cart. Any ideas what am doing wrong here? Have been searching around without finding any solution that works for me.

Solution

With help from @Rohil_PHPBeginner & @WisdmLabs I came to this solution that did the job for me.

global $woocommerce;
// Check if sum
if ($sum_raw < $level3) {
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {

        if ($cart_item['variation_id'] == $product_3) {
            //remove single product
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
Mathias Asberg
  • 3,562
  • 6
  • 30
  • 46
  • WC_Cart::remove_cart_item( $cart_item_key ); – Domain Jun 01 '15 at 09:33
  • Chould i change WC()->cart->remove_cart_item($product_3); for that ? – Mathias Asberg Jun 01 '15 at 09:34
  • What is that variable $product_3? – Domain Jun 01 '15 at 09:41
  • $product_3 = '4753'; , It's the variation ID of the product – Mathias Asberg Jun 01 '15 at 09:43
  • remove_cart_item() it takes parameter as $cart_item_key – Domain Jun 01 '15 at 09:52
  • And how would i do that ? Have been trying many solutions and suggestions but i can't just get it to work. – Mathias Asberg Jun 01 '15 at 10:02
  • @MathiasÅsberg Would you rather post this as Answer, – Rahil Wazir Dec 15 '16 at 19:32
  • Adding this as I found an issue using this code with a simple product instead of a variable product. to make this work in both cases instead of $cart_item['variation_id'] set a variable like so: $prod_id = ( isset( $cart_item['variation_id'] ) && $cart_item['variation_id'] != 0 ) ? $cart_item['variation_id'] : $cart_item['product_id']; This way if there is no variation ID the product ID will still match. – Eolis Mar 28 '18 at 18:47
  • Hopefully, this article https://wpza.net/how-to-remove-cart-items-by-id/ helps, as it discusses both `WC()` and `$woocommerce` methods to use. – WPZA Feb 07 '19 at 20:51

2 Answers2

23

I think you're using remove_cart_item incorrectly. If you go through the documentation, you will find that it accepts cart_item_key as parameter (as wisdmLabs mentioned in comment).

You are using it like so:

WC()->cart->remove_cart_item($product_3);

Try this instead:

WC()->cart->remove_cart_item($cart_item_key);

After updating that line, I think you will able to remove product.

domdambrogia
  • 2,054
  • 24
  • 31
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
6

Use this for latest versions of WooCommerce:

$cartId = WC()->cart->generate_cart_id( 'PUT PRODUCT ID IN HERE' );
$cartItemKey = WC()->cart->find_product_in_cart( $cartId );
WC()->cart->remove_cart_item( $cartItemKey );

replace PRODUCT ID with yours.

Sabrina
  • 2,531
  • 1
  • 32
  • 30