How to write a shopping cart in php? Preferably a code with comments, or a link to a site where this is all described in detail. I searched the entire Internet, but I couldn’t find it anywhere.
Answer 1, authority 100%
I propose this option, which is quite workable and is implemented by a beginner:
Use ajax to add an item to your cart. Save all products in the current session, I don’t think that there will be megabytes from huge lists with products, so we will ignore the base for now or we will use it at the very end when buying.
ajax: You need to teach it to send only the product ID (if any) and provide for updating the number (this is already js) near the icon on the basket.
php: do not forget to pull out the number of products from the session and display it next to the basket. Also enlarge the list. Create a shopping cart and order page (final order form). Write a php code to write the product to the database, where the merchandiser will see the order.
I’ll try to write something sane right here (without tests, but I think the essence will become clear):
On the checkout page, display a list of products (I hope it’s clear where to get the product IDs from), then display the fields for the form. Now what to do with the database or not and what are the pros and cons: If you do it in the database, you need at least a couple of tables (one for the details of the ordering product, and the other is an association with your products and the current parameters of the customer). This approach will force you to create a window with viewing orders in the admin panel. How to do it differently: – without a database. Everything that was ordered and everything that the buyer entered about himself should be sent to the post office.
P.S.: As soon as I decided to write this dregs) This functionality can be implemented in one day even for a beginner. I think it will work at first. Why you don’t need to do something complicated is because you don’t have payments through the site, it’s more complicated there and this approach won’t suit you.
Hmm… And what exactly do you need the functions of the basket and what exactly does not work?
I suspect that it is impossible to write a good and convenient shopping cart in PHP alone, you must use AJAX.
If you are not familiar with this technology, look at the JsHttpRequest object at Dmitry Koterov’s site. It can make your life a lot easier, especially when used in conjunction with the jQuery library.