Hello! I write for the first time the website of an online store and ran into a problem: how to add products to orders, before paying for the order? those. there is a table, for example, Orders , in which goods fall after payment, but how to store these goods until payment? I read that you need to create an intermediate table with the ID of products and orders, but it is not entirely clear how to add the order ID values there, if I haven’t added anything to the Orders table yet? In general, some kind of mess in my head, show some example or resource where you can read about it.
2 answers
There is a basket for this (if I understand you correctly), but its logic may be different ...
I do this:
- for an anonymous user, I put the current goods in the basket in the session;
- for registered
usersinto theuserstable in theproducts_in_cartfield.
When placing an order, depending on who designed it: anonymous or registered user; I transfer the goods either from the session or from the users table, respectively, into the orders table and put 0 in the is_paid field, which indicates that the order has not been paid; after payment, I change the value of the is_paid field in the orders table to 1 , which indicates that the order has been paid ...
You do not need to create a staging table. It is enough to enter the status field in the “orders” table and the “current generated order” field in the “users” table.
Then, if the user desires to start forming an order, we create a new entry in the “orders” table (the default status value should indicate “formed”) and enter the identifier of the created entry in the “current generated order” field. If you pay for this order, reset the field “the current formed order” and translate the order status to “in payment”.
- A little bit wrong, I imagined this process. And if payment is not received, manually delete the record from the database? I wanted to make an intermediate table in order to clean it every month (for example) automatically from all records, but thanks for the answer / advice. - Denis
- oneWhy manually? The criterion of deletion by the value of the status and the date of creation of the order will be sufficient. - ߊߚߤߘ