CookieMonster's avatar

Undefined variable:totalPrice

So I am making a checkout page for my items. When I click checkout, it should calculate all the total price (price of items in my carts) and show the summary page. I deployed it on AWS and it works fine but on my local machine, it kept saying undefined variable:totalPrice

This is snippet of my code in my blade view:

@section('content')
<div style="min-height: 100vh;">
    <div class="container">
        <div class="row">
            <div class="col-12 col-md-8 offset-md-2">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <h5 class="card-title text-center">Offline Payment - Invoice {{ $purchase->purchase_number }}</h5>
                        <div class="row">
                            <div class="col-12">
                                <table class="table table-striped table-bordered">
                                    <tr class="text-center">
                                        <th>Item #</th>
                                        <th>Item Name</th>
                                        <th>Item Price</th>
                                    </tr>

                                    @foreach($purchase->orders as $order)
                                    @foreach($order->items as $item)
                                    <tr class="text-center">
                                        <td>{{ $loop->iteration }}</td>
                                        <td class="text-left">{{ $item->product->parentProduct->name }}</td>
                                        <td class="text-left">RM {{ $item->product->getDecimalPrice() }}</td>
                                        <?php
                                        $totalPrice = 0;
                                        $totalPrice = $totalPrice + $item->product->price;
                                        ?>
                                    </tr>
                                    @endforeach
                                    @endforeach
                                    <tr>
                                        <td colspan="2">Total Price (RM)</td>
                                        <td>
                           ERROR-> 		   {{ number_format(($totalPrice / 100), 2) }}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-12">
                                <form action="/shop/cart/checkout/offline" method="POST">
                                    @csrf
                                    <div class="form-group">
                                        <label for="exampleFormControlFile1">Payment Receipt</label>
                                        <input type="file" class="form-control-file" id="exampleFormControlFile1">
                                        <small class="form-text text-muted">Please provide your payment receipt.</small>
                                    </div>
                                    <div class="text-right">
                                        <button class="btn btn-primary" type="submit">Submit</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

I hightlighted the line that throws the error. I do not understand what wrong I did.

0 likes
10 replies
jlrdw's avatar

May need a \

\number_format(($totalPrice / 100), 2)

But much of this logic could be in a controller.

shez1983's avatar

that wont work either.. you are doing:

  <?php
                                        $totalPrice = 0;
                                        $totalPrice = $totalPrice + $item->product->price;
                                        ?>

so in each loop price = 0... so at best ur total price will be of ONE item.. and total price isnt working because its LOCAL to the foreach loop previously.. what you need to do is put $totalPrice = 0; outside and then it will be accessible by that part.

it is all to do with scopes.. you can use google to read about this concept. BTW i would have an accessor method on the order and calculate order price in there and call that method in here instead of doing it like you have

CookieMonster's avatar

You mean place the initialize total price outside the foreach?

Sorry this code is written by a colleague. I am just trying to debug it.

nfms's avatar

number_format is a built in function not a class name. So adding \ is not the issue here. But yes, this is too much logic to be mixed in with the view.

jlrdw's avatar

Sorry missed

$totalPrice = 0;

was in foreach.

nfms's avatar

Yes, you just need to initialize $totalPrice outside of both foreach loops. You could do this in two ways.

  1. send $totalPrice from the controller initialized to zero. (compact(..., 'totalPrice'));
  2. use @php and @endphp inside of your view instead of . Simply because it is a blade file after all.
CookieMonster's avatar

So I tried to initialize the total price outside the nested for loop and sure the error is gone, but the item price gives the value of 0, if I placed it inside the nested loop, it gives the actual price of the item, say 120.

CookieMonster's avatar

So I tried to initialize the total price outside the nested for loop and sure the error is gone, but the item price gives the value of 0, if I placed it inside the nested loop, it gives the actual price of the item, say 120.

nfms's avatar

Yes. So, you are looping through each orders items. The $item variable is only accessible within the loop it was declared. What are you trying to do?

Please or to participate in this conversation.