dougutuo9879 2016-04-20 07:48
浏览 36

基于单选按钮的结帐表单计算 - Laravel 5.2

i am building an e-commerce website with laravel 5.2

I can add my products to my shopping cart, which is stored in a session. I can view my shopping cart & when i click confirm i go to a checkout page.

Shopping Cart (working as intended): shopping-cart

All my items on the checkout page are still only in the session. Is this good practice or should i store stuff in de database after i go to the checkout page? .

On my checkout page i have a couple of radio-buttons to select the shipping method. I would like to make a calculation based upon my selection. What is the best way to handle this? I would like to avoid putting a submit button below the radio-buttons to confirm the selection. An important notice is that i also have a field for a Coupon code on this page which i calculate using a get request. So refreshing the page is not really an option because my coupon calculation will be gone.

Checkout page: (Radio-buttons are here) enter image description here

2 Options i can think of:

  • Make the calculation on the client side with javascript and add hidden input fields for when the user clicks confirm order they get saved into the database table for orders.

  • Start saving data in de database after the shopping cart, the problem with this approach is that a lot of data is still missing. Like the correct shipping/billing address.

  • 写回答

1条回答 默认 最新

  • doujing6436 2016-04-20 09:52
    关注

    I have recently implemented something like this.

    I have a cart table.

    Cart Table

    and and I have an order table.

    Orders table

    For me it made more sense to have the cart being stored in a table as I could easily associate what was in the cart to an order. Also I didn't have to worry about the session expiring, if they come back weeks later, they will still have their cart in the state they left it.

    When a user completes the purchase I update the cart table with the corresponding order_id and mark it as a complete order.

    It's in the order table where I store billing and shipping information.

    if a user enters a coupon, I update the price of the items in the cart table. I'm handling all coupon and shipping calculation with ajax calls to maintain the state of the page.

    One of the benefits I found of having the cart table is in my User model I could define a relationship that would allow me to quickly get all items in the user cart

    public function cart()
    {
        return $this->hasMany('App\Cart')->where('complete', 0);
    }
    

    which would then allow me to use Laravel's sum functionality on a collection

     // Get the total value of items currently in user's cart.
        $total = $user->cart->sum(function ($item) {
            return $item->price * $item->qty;
        });
    

    Hope this points you somewhat in the right direction.

    Any other questions just give me a shout.

    评论

报告相同问题?

悬赏问题

  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难