you would understand the code better if it was not smashed in with the html
The view is not the place to do this
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have2 types of products in my app. products with attributes and products without. I can successfully add the products to the cart, am having an issue whereby on the part of showing the grand total, only the total of the first 2 products subtotal are calculated and shown.I haven't understood where the bug is. I have tried debugging the code but still can't figure it out. here is the HTML code that processes the grand total price.
//table that shows the cart details and where the subtotal is calculated
<table class="userdatatable table table-striped table-bordered nowrap" style="width:100%; border:2px solid black;">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Discount</th>
<th>Total</th>
<th>Remove</th>
</tr>
</thead>
<body>
<?php $total_price=0; ?>
<?php $attributetotal_price=0; ?>
<?php $noattributetotal_price=0; ?>
@foreach($cartitems as $item)
<tr>
@if ($item->product->is_attribute==1)
<?php $attrpric=Merchadise::getdiscountedattrprice($item['product_id'],$item['size']);
?>,
@else
<?php $discountedprice=Merchadise::getdiscountedprice($item['product_id']);
?>
@endif
<td>{{ $item->product->merch_name }}</td>
@if ($item->product->is_attribute==1)
<td>{{ $attrpric['merch_price'] }}</td>
@else
<td>{{ $item->product->merch_price }}</td>
@endif
<td>
<img src="{{ asset ('images/productimages/small/'.$item->product->merch_image) }}" style="width:100px; height:100px;" alt="Product">
</td>
<td>
<button class="itemupdate qtyminus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
<input data-id={{ $item->id }} class="quantity" min="1" name="quantity[]" value="{{ $item->quantity }}" type="number">
<button class="itemupdate qtyplus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
</td>
@if ($item->product->is_attribute==1)
<td>sh.{{ $attrpric['discount'] * $item['quantity'] }}</td>
<td>sh.{{ $attrpric['final_price'] * $item['quantity'] }}</td>
@elseif($item->product->is_attribute==0)
<td>{{ ($item->product->merch_price-$discountedprice) * $item['quantity'] }}</td>
<td>{{ $discountedprice * $item['quantity'] }}</td>
@endif
<td>
<a class="btn btn-primary btn-xs" onclick="confirm return('Are you Sure You want to Delete?')" href="{{ route('deletecartitem', $item->id) }}"><i class="fa fa-trash"></i></a>
</td>
</tr>
{{-- show cart total --}}
@if ($item->product->is_attribute==1)
<?php $attributetotal_price=$total_price+($attrpric['final_price'] * $item['quantity'] );
?>
@elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$total_price+($discountedprice * $item['quantity'] );?>
@endif
@endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-5 ml-auto">
<div class="cart-page-total">
<h2>Cart totals</h2>
<ul class="mb-20">
<li>Coupon Discount
<span class="couponAmount">
@if (Session::has('couponAmount'))
-Sh.{{ Session::get('couponAmount') }}
@else
sh.0
@endif
</span>
</li>
<li>Grand Total
<span class="grand_total">Sh.{{ $attributetotal_price+$noattributetotal_price-Session::get('couponAmount') }}</span>
</li>
</ul>
@auth
<a href="{{ url('checkout') }}" class="btn btn-success btn-block">Checkout <i class="fa fa-angle-right"></i></a>
@else
<p>To proceed to checkout create or log in to your account...</p>
<span href="#" data-toggle="modal" data-target="#RegistrationModal" class="btn btn-success btn-block">Create/Login an Account<i class="fa fa-angle-right"></i></span>
@endauth
</div>
</div>
</div>
this is how i solved my bug,I removed the $total_price
{{-- show cart total --}}
@if ($item->product->is_attribute==1)
<?php $attributetotal_price=$attributetotal_price+($attrpric['final_price'] * $item['quantity'] );
?>
@elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$noattributetotal_price+($discountedprice * $item['quantity'] );?>
@endif
Please or to participate in this conversation.