簡體   English   中英

如何更新整個(會話)購物車而不是laravel中的單個項目

[英]how to update a whole (session) shopping cart not a single item in laravel

我正在努力爭取這份工作!!!

我有一個在laravel中創建的購物車

這是購物車圖片: 在此處輸入圖片說明

我希望用戶能夠編輯“數量”字段,並且“更新購物車”按鈕應該計算項目“總計”和最終“總計”,但我無法使用

這是我的updatecart方法;

public function updateCart(Request $request)
{
    $cart = $request->session()->get('cart');

    if(!$cart){
        $cart = new Cart($cart);
    }

    $i = 0;
    foreach($cart->items as $item){
        $qty = $request->input($item['item']['id']);
        $item['qty'] = $qty;
        $item['total'] = $item['price'] * $qty;
        $i++;
    }
    $cart->totalQty = array_sum($request->all());

    $request->session()->put('cart', $cart);
    //return $cart->items;
    return redirect()->route('cart');
}

還有我的刀片模板;

<form method="post" action="{{route('updateCart')}}">
  {{ csrf_field() }}
  <table class="table table-hover">
    <thead>
      <tr>
        <th class="text-center">Product</th>
        <th class="text-center">Qty</th>
        <th class="text-center">Rate</th>
        <th class="text-center">Subtotal</th>
        <th> </th>
      </tr>
    </thead>
    <tbody>
      @foreach($items as $item)


      <tr>
        <td class="col-sm-8 col-md-6 text-center">
          <div class="media">
            <a class="thumbnail pull-left" href="#"> <img class="media-object" src="{{asset('storage/'.$item['image'])}}" style="width: 100px; height: 72px;"> </a>
            <div class="media-body">
              <h4 class="media-heading"><a href="#">{{$item['item']['title']}}</a></h4>
            </div>
          </div>
        </td>
        <td class="col-sm-1 col-md-1" style="text-align: center">
          <input type="number" class="form-control input-sm" name="{{$item['item']['id']}}" value="{{old($item['item']['id']) ? old($item['item']['id']) : $item['qty']}}">
        </td>
        <td class="col-sm-1 col-md-1 text-center">&#x20a6;{{$item['price']}}</td>
        <td class="col-sm-1 col-md-1 text-center"><strong>&#x20a6;{{$item['total']}}</strong></td>
        <td class="col-sm-1 col-md-1">
          <a href="#"> <button type="button" class="btn btn-danger">
                                    <span class="fa fa-remove"></span> Remove
                                </button>
          </a>
        </td>
      </tr>
      @endforeach

      <tr>
        <td> </td>
        <td> </td>
        <td>
          <h3>Total</h3>
        </td>
        <td class="text-center">
          <h3><strong>&#x20a6;{{$total}}</strong></h3>
        </td>
        <td></td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> <button type="submit" class="btn btn-default">
                                <span class="glyphicon glyphicon-shopping-cart"></span>Update shopping Cart
                            </button> </td>
        <td>
          <a href="/"> <button type="button" class="btn btn-default">
                                <span class="glyphicon glyphicon-shopping-cart"></span> Continue Shopping
                            </button>
          </a>
        </td>
        <td>
          <a href={{route( 'checkout')}} class="btn btn-success" role="button">
                           Checkout <span class="glyphicon glyphicon-play"></span>
                        </a>
        </td>
      </tr>
    </tbody>
  </table>
</form>

先感謝您!

編輯:

請注意,我不想更新每個商品的購物車,而是整個購物車!

您可以在插入購物車之前使用session()->forget('cart')

$cart = $request->session()->forget('cart');

if(!$cart){
    $cart = new Cart($cart);
}

$i = 0;
foreach($cart->items as $item){
    $qty = $request->input($item['item']['id']);
    $item['qty'] = $qty;
    $item['total'] = $item['price'] * $qty;
    $i++;
}
$cart->totalQty = array_sum($request->all());

$request->session()->put('cart', $cart);

取出后,再次插入購物車。 它將在您的情況下工作

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM