Moltin Cart与Laravel,添加了多个项目

时间:2014-10-28 16:34:19

标签: php laravel-4 shopping-cart

我正在使用Laravel和Moltin laravel-cart包并且有一个关于它的问题一切正常但是当我添加多个项目时,购物车总更新但不显示该项目。

我将以下内容添加到购物车中:

{{ 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>&pound;{{ $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>&pound;{{ $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>&pound;{{ 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>

但就像我说它只显示一个项目,但是£中的金额是正确的。

1 个答案:

答案 0 :(得分:1)

我认为您忘记了正确包含每件商品的数量。您的@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'));
}