【发布时间】:2017-04-19 03:03:55
【问题描述】:
我在laravel中使用购物车功能,我已经成功使用了Cart :: add功能,但是使用Total功能失败时,总金额大于我计算的金额。
有什么建议吗?谢谢!
这是我的代码 MyController
public function muahang($id){
$product_buy = DB::table('products')->where('id',$id)->first();
Cart::add(array('id'=>$id,'name' =>$product_buy->name,'qty'=>1,'price'=>$product_buy->price,'options'=>array('img'=>$product_buy->image)));
$content = Cart::content();
return redirect()->route('giohang');
}
public function giohang(){
$content = Cart::content();
$total = Cart::total();
return view('user.pages.shopping',compact('content','total'));
}
这是我的看法:
@foreach($content as $content_item)
<tr>
<td class="image">
<a href="#"><img title="product" alt="product" src="{{ asset('resources/upload/'.$content_item->options->img) }}" height="50" width="50"></a>
</td>
<td class="name"><a href="#">{{ $content_item->name }}</a></td>
<td class="quantity"><input type="text" size="1" value="{{ $content_item->qty }}" name="quantity[40]" class="span1"></td>
<td class="total"> <a href="#"><img class="tooltip-test" data-original-title="Update" src="{!! asset('public/user/img/update.png') !!}" alt=""></a>
<a href="#"><img class="tooltip-test" data-original-title="Remove" src="{!! asset('public/user/img/remove.png') !!}" alt=""></a>
</td>
<td class="price">{{ number_format($content_item->price,0,',','.') }} </td>
<td class="total">{{ number_format(($content_item->price * $content_item->qty),0,',','.') }} </td>
</tr>
@endforeach
</table>
</div>
<div class="container">
<div class="pull-right">
<div class="span4 pull-right">
<table class="table table-striped table-bordered ">
<tr>
<td><span class="extra bold totalamout">Total :</span></td>
<td><span class="bold totalamout">{!! $total !!}</span></td>
</tr>
</table>
<input type="submit" value="CheckOut" class="btn btn-orange pull-right">
<input type="submit" value="Continue Shopping" class="btn btn-orange pull-right mr10">
</div>
</div>
</div>
</div>
【问题讨论】:
-
total() 方法基本上计算购物车中所有商品的总和,给定价格和数量。
标签: php laravel shopping-cart