【问题标题】:Using Crinsane/LaravelShoppingcart updating items w/Laravel 4使用 Crinsane/LaravelShoppingcart 更新项目 w/Laravel 4
【发布时间】:2014-04-26 19:57:00
【问题描述】:

我正在使用https://github.com/Crinsane/LaravelShoppingcart,一切正常,但我的电子商务网站正在动态地从数据库中提取数据。所以我的问题是我将如何编辑下面的代码来说明是否将另一个项目添加到购物车然后添加它。

public function add_to_cart()
{
    $page = Pages::all();
    $input = Input::all();

    $price = preg_replace('/[^0-9]+/', '', $input['options']);

    if (!empty($input)) {
        $cartContents = Cart::add(array('id' => randomNumber(), 'name' => $input['product_name'], 'qty' => 1, 'price' => $price));
        $carts = Cart::content();

        $cart_session = Cart::count();
        Session::put('cart_sess', $cart_session);
    }
    return View::make('basket.cart', compact('page', 'cartContents', 'carts'));
}

如上述方法所示,它添加了一项,但我似乎无法弄清楚如何添加多个项目并添加交付,然后将它们全部加到客户将支付的实际总额中。

还有其他人玩弄这个包并设法让它工作吗?

非常感谢您的帮助。

【问题讨论】:

    标签: laravel-4 shopping-cart


    【解决方案1】:

    这可能不是最好的答案,但它会完成这项工作。我对你的问题也有不同的方法 在您看来,请确保您有一个覆盖您的表格的表单以及全部更新按钮。 “继续结帐”可能只是一个按钮,因为您的购物车只是一个会话。

    // View (notice how the name of the qty has the rowid):
    {!! Form::open(array('route' => 'cart-update', 'id' => 'cart-form')) !!}
    ...
    <td>
        <div class="cart_quantity_button">
           <input class="cart_quantity_input" type="number" name="quantity_{{ $row->rowid }}" min="1" max="10000" step="1" value="{{$row->qty}}" autocomplete="off" size="2">
        </div>
    </td>
    ...
    <button type="submit" class="btn-black pull-right" id="update_shopping">Update Shopping Cart</button>
    {!! Form::close() }}
    
    // Routes
    Route::post('cart/update/', array('as' => 'cart-update', 'uses' => 'CartController@updateAll'));
    
    // Controller
    use Cart;
    public function updateAll(Request $request)
    {
        foreach($request->all() as $k => $v)
        {
            if ($k == "_token") continue;
            $arr = explode("_", $k);
            // Update the cart -> Cart::update($rowid, $qty);
            Cart::update($arr[1], $v);
        }
    
        $cart = Cart::content();
        return View::make('shoppingcart', compact('cart'));
    
    }
    

    有更好的方法可以做到这一点,但这应该可以满足您的需求。好吧,我希望

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2017-01-07
      • 2017-03-31
      • 2019-06-20
      • 1970-01-01
      相关资源
      最近更新 更多