Codeigniter Cart - saving data in database - how to approach?

此生再无相见时 提交于 2019-12-22 12:46:27

问题


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);


来源:https://stackoverflow.com/questions/13618379/codeigniter-cart-saving-data-in-database-how-to-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!