Hi, I have some products in my cart
I want the prices of all products together with the deduction of the discount amount together
Sum and save in the final price field in the card table. How can this be done?
public function getPriceAfterApplyCoupon()
{
foreach ($this->categories as $category) {
$coupons = $category->validCoupons();
if ($coupons->isNotEmpty()) {
$amount = $this->price;
$discount = ($amount * $coupons[0]->percent) / 100;
$total = $amount - $discount;
foreach ($this->carts as $cart) {
$cart->coupon_id = $coupons[0]->id;
//this code not working
$cart->final_price + $total;
$cart->save();
}
return number_format($total);
}
}
return $this->price;
}
Ok so I dont know the full context here and its kind of confusing with caregories and coupons looping before you loop over the products.
but maybe you should be doing something like this
I have a collection of courses on my card
Some of these courses are discounted
Some others do not have a discount
Now I want to add the prices of these courses together and save them in the final price field on the card
so thats why i recommended that you start with a foreach loop on cart
foreach($this->cart as $cartItem) {
$cartTotal = 0;
// check if this cart Item has discount
// check for discount coupons
// if yes . Apply the discount
// $cartItem->price = $cartItem->price() - $discount;
// Add total to $cartTotal
// $cartTotal += $cartItem->price
}
// outside the loop
return $cartTotal ;