php - Remove item with $product_id - Woocommerce -


made function customer product added cart when reach specific amount.

example of when customer reaches level 3 , product added.

// bonus products $product_1 = '4751';  $product_2 = '4752';  $product_3 = '4753';   // cart value in 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 sum level  $level3 = '1500';  // check sum , apply product if ($sum_raw >= $level3) {  // cycle through each product in cart , check match $found = 'false'; foreach (wc()->cart->cart_contents $item) {     global $product;     $product_id = $item['variation_id'];      if ($product_id == $product_3) {         $found = 'true';     } }  // if product found nothing  if ($found == 'true') {} // else add else {     //we add product     wc()->cart->add_to_cart($product_3); 

if customer decides remove item's statement true want able remove again.

if ($sum_raw < $level3) {      // trying remove item     foreach ($woocommerce->cart->get_cart() $cart_item_key => $cart_item) {         if ($cart_item['variation_id'] == $product_3) {              //remove single product             wc()->cart->remove_cart_item($product_3);         }     } } 

am not manage remove product cart. ideas doing wrong here? have been searching around without finding solution works me.

solution

with @rohil_phpbeginner & @wisdmlabs came solution did job me.

global $woocommerce; // check if sum if ($sum_raw < $level3) {     foreach ($woocommerce->cart->get_cart() $cart_item_key => $cart_item) {          if ($cart_item['variation_id'] == $product_3) {             //remove single product             $woocommerce->cart->remove_cart_item($cart_item_key);         }     } } 

i think using remove_cart_item wrongly. if go through documentation, find accepts cart_item_key parameter(as wisdmlabs mentioned in comment).

and using wc()->cart->remove_cart_item($product_3); instead of wc()->cart->remove_cart_item($cart_item_key);

so replacing line, think able remove product.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -