{{-- Note: This whole file is 'include'-ed in front/products/cart.blade.php (to allow the AJAX call when updating orders quantities in the Cart) --}}
{{-- We'll place this $total_price inside the foreach loop to calculate the total price of all products in Cart. Check the end of the next foreach loop before @endforeach --}} @php $total_price = 0 @endphp @foreach ($getCartItems as $item) {{-- $getCartItems is passed in from cart() method in Front/ProductsController.php --}} @php $getDiscountAttributePrice = \App\Models\Product::getDiscountAttributePrice($item['product_id'], $item['size']); // from the `products_attributes` table, not the `products` table // dd($getDiscountAttributePrice); @endphp {{-- This is placed here INSIDE the foreach loop to calculate the total price of all products in Cart --}} @php $total_price = $total_price + ($getDiscountAttributePrice['final_price'] * $item['quantity']) @endphp @endforeach
Product Price Quantity Subtotal Action
@if ($getDiscountAttributePrice['discount'] > 0) {{-- If there's a discount on the price, show the price before (the original price) and after (the new price) the discount --}}
EGP{{ $getDiscountAttributePrice['final_price'] }}
EGP{{ $getDiscountAttributePrice['product_price'] }}
@else {{-- if there's no discount on the price, show the original price --}}
EGP{{ $getDiscountAttributePrice['final_price'] }}
@endif
+ {{-- The Plus sign: Increase items by 1 --}} {{-- .updateCartItem CSS class and the Custom HTML attributes data-cartid & data-qty are used to make the AJAX call in front/js/custom.js --}} - {{-- The Minus sign: Decrease items by 1 --}} {{-- .updateCartItem CSS class and the Custom HTML attributes data-cartid & data-qty are used to make the AJAX call in front/js/custom.js --}}
EGP{{ $getDiscountAttributePrice['final_price'] * $item['quantity'] }} {{-- price of all products (after discount (if any)) (= price (after discoutn) * no. of products) --}}
{{-- --}} {{-- .deleteCartItem CSS class and the Custom HTML attribute data-cartid is used to make the AJAX call in front/js/custom.js --}}
{{-- To solve the problem of Submiting the Coupon Code works only once, we moved the Coupon part from cart_items.blade.php to here in cart.blade.php --}} {{-- Explanation of the problem: http://publicvoidlife.blogspot.com/2014/03/on-on-or-event-delegation-explained.html --}}
Cart Totals

Sub Total

{{-- Total Price before any Coupon discounts --}}
EGP{{ $total_price }}

Coupon Discount

{{-- We create the 'couponAmount' CSS class to use it as a handle for AJAX inside $('#applyCoupon').submit(); function in front/js/custom.js --}} @if (\Illuminate\Support\Facades\Session::has('couponAmount')) {{-- We stored the 'couponAmount' in a Session Variable inside the applyCoupon() method in Front/ProductsController.php --}} EGP{{ \Illuminate\Support\Facades\Session::get('couponAmount') }} @else EGP0 @endif

Grand Total

{{-- Total Price after Coupon discounts (if any) --}}
EGP{{ $total_price - \Illuminate\Support\Facades\Session::get('couponAmount') }} {{-- We create the 'grand_total' CSS class to use it as a handle for AJAX inside $('#applyCoupon').submit(); function in front/js/custom.js --}} {{-- We stored the 'couponAmount' a Session Variable inside the applyCoupon() method in Front/ProductsController.php --}}