set_price() not working in nested foreach
-
I am trying to set the price of a product to 0 in a nested foreach loop. Maybe I am thinking about the whole problem wrong, but I can’t get it to work. If I do
$cart_item['data']->set_price('0');
at the top of the first foreach, it works (and sets the price to 0 for all products).But when doing it inside the last foreach, near the bottom of the code block below, nothing happens. The price is not visually set in the cart. I have checked the cart item object, and the
changes:protected
part of it is updated with the new price of 0.I have also checked that I actually reach that line of code. What I want is to set the price of the split item to 0, but not the other items that are not split.
What am I doing wrong?
if (is_admin() && !wp_doing_ajax()) { return; } if (did_action('woocommerce_before_calculate_totals') > 1) { return; } $compatible = false; $thecart = $cart->get_cart(); //Check if we have any compatible items foreach ($thecart as $cart_item) { $postid = $cart_item['product_id']; $quantity = $cart_item['quantity']; //Check if the item is a 3for2 item if (!$this->get_3for2_status($postid)) { //Not a 3for2 item continue; } //If yes, check if the amount added is compatible with the 3 for 2 discount if ($quantity < 3) { //Not compatible with discount continue; } //Set compatible flag to true $compatible = true; } //Don't continue if we have no compatible products if ($compatible != true) { return; } //Get number of items to be free $quantity = 0; foreach ($thecart as $cart_item) { //Check if the item is a 3for2 item if (!$this->get_3for2_status($postid)) { //Not a 3for2 item continue; } $quantity += $cart_item['quantity']; } $freequantity = intdiv($quantity, 3); //Loop the cart to split free items from the cheapest item //Do this while we still have free items to give while ($freequantity > 0) { $pricearray = []; foreach ($thecart as $cart_item) { $id = $cart_item['product_id']; $price = $cart_item['data']->get_price(); //Don't add free products if ($price > 0) { $pricearray[$id] = $price; } } //Split the lowest priced item into a free one $splititem = array_search(min($pricearray), $pricearray); foreach ($thecart as $cart_item_key => $cart_item) { $id = $cart_item['product_id']; //Only check the lowest priced item if ($id != $splititem) { continue; } $quantity = $cart_item['quantity']; //If there is only one left, set price ... if ($quantity == 1) { $cart_item['data']->set_price('0'); continue; } //... otherwise, split item //Set quantity to quantity - 1 for the item $cart->set_quantity($cart_item_key, $quantity - 1); //Then split 1 of the item //Set a unique key. $cart_item['unique_key'] = uniqid(); //Store the unique key $uniqkey = $cart_item['unique_key']; //Get variation vars $variation_id = $cart_item['variation_id']; $variation = $cart_item['variation']; //Add the product as a new line item with the same variations that were passed $cart->add_to_cart($id, 1, $variation_id, $variation, $cart_item); //Do a new fetch of the cart and loop it again foreach ($cart->get_cart() as $cart_item) { //Check that we handle the correct item if ($cart_item['unique_key'] != $uniqkey) { continue; } //Set the price to be free $cart_item['data']->set_price('0'); } } $freequantity -= 1; }
- The topic ‘set_price() not working in nested foreach’ is closed to new replies.