简体   繁体   中英

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

I having struggling for sometime to get this work!!!

I have a shopping cart created in laravel

This is the Shopping Cart image : 在此处输入图片说明

I want user to be able to edit the 'qty' field and 'update shopping cart' button should compute the item 'total' and final 'total' but I can't get to it work

here is my updatecart method;

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');
}

and my blade template;

<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>

Thank you in advance!

Edited:

please note I don't want to update the cart per item but the whole cart!

You can use session()->forget('cart') before inserting into you 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);

After removing insert again into cart. It will work in your scenario

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM