【问题标题】:How to use total function in Laravel Shopping cart如何在 Laravel 购物车中使用总功能
【发布时间】:2017-04-19 03:03:55
【问题描述】:

我在laravel中使用购物车功能,我已经成功使用了Cart :: add功能,但是使用Total功能失败时,总金额大于我计算的金额。

有什么建议吗?谢谢!

这是我的代码 MyController

public function muahang($id){
        $product_buy = DB::table('products')->where('id',$id)->first();
        Cart::add(array('id'=>$id,'name' =>$product_buy->name,'qty'=>1,'price'=>$product_buy->price,'options'=>array('img'=>$product_buy->image)));
        $content = Cart::content();
        return redirect()->route('giohang');
    }
    public function giohang(){
        $content = Cart::content();
        $total = Cart::total();
        return view('user.pages.shopping',compact('content','total'));
    }

这是我的看法:

@foreach($content as $content_item)
          <tr>
            <td class="image">
              <a href="#"><img title="product" alt="product" src="{{ asset('resources/upload/'.$content_item->options->img) }}" height="50" width="50"></a>
            </td>
            <td  class="name"><a href="#">{{ $content_item->name }}</a></td>
            <td class="quantity"><input type="text" size="1" value="{{ $content_item->qty }}" name="quantity[40]" class="span1"></td>
            <td class="total"> <a href="#"><img class="tooltip-test" data-original-title="Update" src="{!! asset('public/user/img/update.png') !!}" alt=""></a>
              <a href="#"><img class="tooltip-test" data-original-title="Remove"  src="{!! asset('public/user/img/remove.png') !!}" alt=""></a>
            </td>
            <td class="price">{{ number_format($content_item->price,0,',','.') }} </td>
            <td class="total">{{ number_format(($content_item->price * $content_item->qty),0,',','.') }} </td> 
          </tr>
          @endforeach
        </table>
      </div>
      <div class="container">
      <div class="pull-right">
          <div class="span4 pull-right">
            <table class="table table-striped table-bordered ">
              <tr>
                <td><span class="extra bold totalamout">Total :</span></td>
                <td><span class="bold totalamout">{!! $total  !!}</span></td>
              </tr>
            </table>
            <input type="submit" value="CheckOut" class="btn btn-orange pull-right">
            <input type="submit" value="Continue Shopping" class="btn btn-orange pull-right mr10">
          </div>
        </div>
        </div>
    </div>

【问题讨论】:

  • total() 方法基本上计算购物车中所有商品的总和,给定价格和数量。

标签: php laravel shopping-cart


【解决方案1】:

建议:

因为这是购物车选项的问题。第三方包 gloudemans 可用于购物车。这适用于 laravel。

下面提供了有关 gloudemans 安装和使用的所有正确信息

https://github.com/Crinsane/LaravelShoppingcart

希望它是可行的。

【讨论】:

    【解决方案2】:

    改变

    $total = Cart::total();
    

     $total = Cart::subtotal();
    

    【讨论】:

      【解决方案3】:

      如果你使用的是这个包?

      AvoRed E commerce

      即使在您的视图文件中,您也可以轻松完成。

      {{ 购物车::total() }}

      【讨论】:

        【解决方案4】:

        您可以使用sum() 方法。 sum 方法返回集合中所有项目的总和:Docs

        在您的购物车模型中

        public function total()
        {
            return $this->products->sum('price');
        }
        

        view.blade

        Cart total: {{ $cart->total() }}
        // Total: 150 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-02
          • 2011-12-19
          • 1970-01-01
          • 2020-11-06
          • 2019-06-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多