【问题标题】:How to add products from different table in one cart using Gloudemans/shoppingcart?如何使用 Gloudemans/shoppingcart 将不同表的产品添加到一个购物车中?
【发布时间】:2018-03-15 04:52:27
【问题描述】:

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

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 包 -> https://packagist.org/packages/gloudemans/shoppingcart

【问题讨论】:

    标签: php laravel-5 laravel-5.4


    【解决方案1】:

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

    Cart::add([
      ['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
      ['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 
    'options' => ['size' => 'large']]
    ]);
    
    Cart::add([$product1, $product2]);
    

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多