Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove item with $product_id - Woocommerce

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);
        }
    }
}
like image 394
Mathias Asberg Avatar asked Jun 01 '15 09:06

Mathias Asberg


2 Answers

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.

like image 60
Rohil_PHPBeginner Avatar answered Oct 27 '22 09:10

Rohil_PHPBeginner


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.

like image 21
Sabrina Avatar answered Oct 27 '22 10:10

Sabrina