Codeigniter Cart - saving data in database - how t

2019-05-26 17:47发布

问题:

I need help with handling orders and cart in my web application.

I decided to use Cart library built in Codeigniter 2.

I've seen some tutorials about that Cart library and i know how to use it, but i dont know:

  • when shall I create/save that order in database? when user is adding items to cart, or shall i keep all order data in session and "move" it do database when user accepts order?
  • shall I store session id or something other in database?

I tried to see how cart/order functionality is implemented in PrestaShop, but it looks too complicated for me, im amateur PHP programmer.

回答1:

The ideal and good way to use cart is keep it in session, codeigniter's cart class is doing the same thing, and when user gives order use this data put this order in database and do other stuff like payment gateway, shipping. If you want to use user to keep his order in next session, like if user add some product in cart and he quits before giving order and he is a registered user, then you can save his cart every time in database, so that if he gone away without putting order you can show him his orders next time when he logged in.

You can store users cart data in database using $this->cart->contents(); method of cart class. use like this

$cartContentString = serialize($this->cart->contents());

you will get a string of cart content, you can save this string in database and later use it like

$cartArray = unserialize($cartContentString);