基于单选按钮的结帐表单计算 - 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.

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.