我正在使用Laravel和Moltin Laravel购物车包,对此有一个问题,但当我添加多个项目时,购物车总数会更新,但不会显示该项目。
我有以下内容添加到购物车中:
{{ Form::open(['route' => 'cart']) }}
<input type="hidden" name="path" value="{{ Request::path() }}">
<input type="hidden" name="image" value="{{ $item->image }}">
<input type="hidden" name="product" value="{{ $item->name }}">
<input type="hidden" name="description" value="{{ $item->seo_description }}">
<input type="hidden" name="qty" value="1">
<input type="hidden" name="size" value="{{ Session::get('size') }}">
<input type="hidden" name="colour" value="{{ Session::get('colour') }}">
<input type="hidden" name="price" value="{{ $item->price }}">
@if ($item->stock > 0)
<button class="btn btn-success">Add to Bag</button>
@else
<a href="" class="btn btn-primary">Email us</a>
@endif
{{ Form::close() }}
然后我有这个显示推车上的物品。
@foreach($items as $item)
<tr>
<td class="col-sm-8 col-md-6">
<div class="media">
<span class="thumbnail pull-left">
<img class="media-object" src="/uploads/product-images/{{$item->image}}" style="width: 72px; height: 72px;">
</span>
<div class="media-body">
<h4 class="media-heading">
<a href="{{ $item->path }}">{{ $item->name }}</a>
</h4>
<span>Status: </span><span class="text-success"><strong>In Stock</strong></span>
</div>
</div>
</td>
<td class="col-sm-1 col-md-1" style="text-align: center">
<input type="email" class="form-control" id="exampleInputEmail1" value="1">
</td>
<td class="col-sm-1 col-md-1 text-center"><strong>£{{ $item->price }}</strong></td>
<td class="col-sm-1 col-md-1">
</td>
</tr>
@endforeach
<tr>
<td></td>
<td></td>
<td></td>
<td><h5>Subtotal</h5></td>
<td class="text-right"><h5><strong>£{{ $item->price }}</strong></h5></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><h3>Total</h3></td>
<td class="text-right"><h3><strong>£{{ Cart::total(false) }}</strong></h3></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<a href="/remove/{{ $item->identifier }}" class="btn btn-danger">
<span class="glyphicon glyphicon-remove"></span> Remove
</a>
</td>
<td>
<a href="" class="btn btn-default">
<span class="glyphicon glyphicon-shopping-cart"></span> Continue Shopping
</a>
</td>
<td>
<a href="/checkout" class="btn btn-success">
Checkout <span class="glyphicon glyphicon-play"></span>
</a>
</td>
</tr>
但正如我所说,它只显示了一项,但以英镑为单位的金额是正确的。
我想你忘了正确地包括每件物品的数量。你的@foreach
:中有一条完全不合适的线
<input type="email" class="form-control" id="exampleInputEmail1" value="1">
我认为应该是:
<input type="text" class="form-control" value="{{ $item->qty }}">
编辑
Cart::insert()
不增加数量,它只是将一个项目添加到购物车中,并用指定的数量覆盖数量。添加商品时,您需要检查商品是否已在购物车中,并相应地更新数量。为此,添加到购物车中的每个商品都必须有一个唯一的ID(到目前为止,我看到你正在将1
设置为ID,这将不起作用,因为购物车使用ID来区分不同的产品。所以你的代码应该是这样的:
public function add() {
$input = Input::all();
// Pass the product ID with the request parameters
$id = $input['id'];
// Try to get the cart item by ID
$item = Cart::item($id)
// If the result if false then the items was not found
// in the cart and you need to create a new entry
if ($item === false)
{
$product = array(
'id' => $id,
'name' => $input['product'],
'price' => $input['price'],
'colour' => $input['colour'],
'quantity' => $input['qty'],
'image' => $input['image'],
'path' => $input['path'],
'description' => $input['description']
);
}
else
{
// If it was found you just need to update the quantity
$item->quantity += (int) $input['qty'];
$product = $item;
}
Cart::insert($product);
$items = Cart::contents();
return View::make('cart.bag', compact('items'));
}