laravel - Laravel 购物车没有收取食品价格
问题描述
正在为餐馆构建一个订购系统当单击此处订购时,它会将用户带到购物车页面>这是将用户带到购物车页面的链接
<p class="btn-holder"><a href="{{ route('restaurants.cart',$restaurant->id)}}" class="btn btn-primary btn-block text-center" role="button" > Order this</a> </p>
但我的购物车页面没有显示食品价格。它显示为 0 但价格存在。
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Price</th>
<th style="width:8%">Quantity</th>
<th style="width:22%" class="text-center">Subtotal</th>
<th style="width:10%"></th>
</tr>
</thead>
<tbody>
<?php $total = 0 ?>
@if(session('cart'))
@foreach(session('cart') as $id => $details)
<?php $total += (int)$details['price'] *(int)$details['quantity'] ?>
<tr>
<td data-th="Product">
<div class="row">
{{-- <div class="col-sm-3 hidden-xs"><img src="{{ $details['photo'] }}" width="100" height="100" class="img-responsive"/></div> --}}
<div class="col-sm-9">
<h4 class="nomargin">{{ $details['name'] }}</h4>
</div>
</div>
</td>
<td data-th="Price">{{(int) $details['price'] }}</td>
<td data-th="Quantity">
<input type="number" value="{{ $details['quantity'] }}" class="form-control quantity" />
</td>
<td data-th="Subtotal" class="text-center">{{(int) $details['price'] *(int) $details['quantity'] }}</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm update-cart" data-id="{{ $id }}"><i class="fa fa-refresh"></i></button>
<button class="btn btn-danger btn-sm remove-from-cart" data-id="{{ $id }}"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
@endforeach
@endif
</tbody>
<tfoot>
<tr class="visible-xs">
<td class="text-center"><strong>Total {{ $total }}</strong></td>
</tr>
<tr>
<td><a href="{{ url('/') }}" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a></td>
<td colspan="2" class="hidden-xs"></td>
<td class="hidden-xs text-center"><strong>Total ${{ $total }}</strong></td>
</tr>
</tfoot>
</table>
@endsection
这是 Foods 控制器和 addToCart 函数,用于将商品添加到购物车
public function addToCart($id){
$food = Food::find($id);
if(!$food) {
abort(404);
}
$cart = session()->get('cart');
// if cart is empty then this the first product
if(!$cart) {
$cart = [
$id => [
"name" => $food->food_item,
"quantity" => 1,
"price" => $food->price,
]
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if cart not empty then check if this product exist then increment quantity
if(isset($cart[$id])) {
$cart[$id]['quantity']++;
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if item not exist in cart then add to cart with quantity = 1
$cart[$id] = [
"name" => $food->food_item,
"quantity" => 1,
"price" => $food->price,
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
}
解决方案
推荐阅读
- azure-active-directory - 使用 OAuth2.0 从 Office365 检索电子邮件
- swift - 将内联显式类型注释添加到具有返回值但在 Swift 中没有输入参数的闭包?
- flutter - ListView 或 BLoC 的 Flutter 状态恢复
- haskell - 我可以在 Haskell 中打印多态函数的类型,就像我将一个具体类型的实体传递给它一样吗?
- wordpress - Wordpress 子域上的 CORS 策略字体错误
- ruby - 使用 Ruby On Capybara 关闭来自 Salesfroce 的框架“标签”
- python - Django 中的通用模板模块存在问题,不允许我运行实时服务器
- apache-spark - Pyspark:将多列转换为数字
- python - 用将索引作为参数的对象填充 2d numpy 数组
- html - 我似乎无法弄清楚如何在调整页面大小时阻止我的 html 表单分裂和向下移动