如何使用Gloudemans / shoppingcart在一个购物车中添加来自不同餐桌的产品?

时间:2017-08-26 11:43:08

标签: php laravel-5 laravel-5.4

我想从两个表中添加产品,一个来自项目,另一个来自子项目。 但是当我尝试从相关的父项添加子项时。父项本身已添加到购物车中。数量变成双倍。 冲突是......让我们假设父母的产品有id = 1,2,3,...... 在子表中也有产品也具有相同的id&s; s = 1,2,3,... 当我尝试添加id = 1的产品时。 它将其作为父项并添加数量为double的相同父产品。 我不知道如何解决这个问题。 我真的对此感到沮丧。 请看下面的代码。 子项目的方法如下!

public function show($s)
 {
   $sId = Item::find($s);
   Cart::add($sId->id,$sId->sub_title,1,$sId->sub_price);
   return back();
 }

来自父项的方法如下:

public function edit($mealId)
{
  $getId = Menu::find($mealId);
  Cart::add($getId->id,$getId->title,1,$getId->price);
  return back();
}

在购物车索引页面上:

 @extends('layouts.app')

@section('title', 'Cart')
<style media="screen">
a.cross-btn:link{
  text-decoration: none;
}
div.cart-section{
  width: 100%;
  height: auto;
  padding: 40px 25px;
}
</style>
@section('body')

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div class="cart-section">
        <div class="table-responsive">
          <table class="table table-hover">
            <tr>
              <th>Item Name</th>
              <th>Qty</th>
              <th>Total Price</th>
              <th>Remove Item</th>
            </tr>
            @foreach($cartItems as $cartItem)
            <tr>
              <td>{{$cartItem->name}}</td>
              <td>{{$cartItem->qty}}</td>
              <td>{{$cartItem->price * $cartItem->qty}} $</td>
              <td>
                <form action="{{url('cart/'.$cartItem->rowId)}}" method="post">
                  {{csrf_field()}}
                  {{method_field('DELETE')}}
                  <input type="submit" name="submit" value="&#10006;" class="btn btn-danger"/>
                </form>
              </td>
            </tr>
            @endforeach
            @if(Cart::subtotal() != 0)
            <tr>
              <td><strong>Sub Total</strong></td>
              <td>{{Cart::subtotal()}} $</td>
              @if (Auth::guest())
              <td><a href="{{route('login')}}" type="button" class="btn btn-default">Login</a> OR Order as Guest
                <form action="{{route('method')}}" method="get">
                  {{csrf_field()}}
                  <legend>Payment Method</legend>
                  COD: <input type="radio" name="payment_method" value="Cash On Delivery" checked required />
                  Braintree: <input type="radio" name="payment_method" value="Braintree" required />
                  <input type="submit" name="submit" class="btn btn-success" value="Order as Guest" />
                </form>
                {{--<a href="{{route('guestOrder')}}">Order as Guest</a>--}}
              </td>
              @else
              <td>
                <form action="#" method="post">
                  {{csrf_field()}}
                  <legend>Payment Method</legend>
                  COD: <input type="radio" name="payment_method" value="Cash On Delivery" checked required />
                  Braintree: <input type="radio" name="payment_method" value="Braintree" required />
                  <input type="submit" name="submit" class="btn btn-success" value="Proceed to Checkout" />
                </form>
              </td>
              @endif
            </tr>
            @else
            <tr>
              <td><a href="{{route('menu')}}" type="button" class="btn btn-danger">Continue Shopping</a></td>
            </tr>
            @endif
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

@endsection

参考我使用这个laravel包 - &gt; https://packagist.org/packages/gloudemans/shoppingcart

1 个答案:

答案 0 :(得分:1)

向购物车添加多个商品时,add()方法将返回CartItems数组。

libsystem_kernel.dylib`__pthread_kill:
    0x182db42e0 <+0>:  mov    x16, #0x148
    0x182db42e4 <+4>:  svc    #0x80
->  0x182db42e8 <+8>:  b.lo   0x182db4300               ; <+32>
    0x182db42ec <+12>: stp    x29, x30, [sp, #-0x10]!
    0x182db42f0 <+16>: mov    x29, sp
    0x182db42f4 <+20>: bl     0x182d94bdc               ; cerror_nocancel
    0x182db42f8 <+24>: mov    sp, x29
    0x182db42fc <+28>: ldp    x29, x30, [sp], #0x10
    0x182db4300 <+32>: ret