【问题标题】:Moltin Cart with Laravel, adding more than one item带有 Laravel 的 Moltin Cart,添加了多个项目
【发布时间】:2014-12-24 04:06:32
【问题描述】:

我正在使用 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>

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

【问题讨论】:

  • 有人遇到过同样的问题吗?

标签: php laravel-4 shopping-cart


【解决方案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'));
}

【讨论】:

  • 即便如此,表格列表中仍然只显示一个。
  • 我做了一个 var_dump 并且只看到一个项目,即使那里有 3 个。
  • 请发布您负责更新购物车商品的路由/控制器的代码。
  • 我只有这个方法:public function add() { $input = Input::all(); $product = array('id' => 1, 'name' => $input['product'], 'price' => $input['price'], 'color' => $input['colour'] , '数量' => $input['qty'], 'image' => $input['image'], 'path' => $input['path'], 'description' => $input['description '] );购物车::插入($product); $items = 购物车::contents(); return View::make('cart.bag', compact('items')); }
  • 这就是我需要的部分 $item->quantity += (int) $input['qty']; $产品 = $项目;非常感谢
猜你喜欢
  • 2015-09-15
  • 2014-03-01
  • 2014-04-19
  • 2015-02-01
  • 2018-07-24
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
相关资源
最近更新 更多