“Product” records that hold info about existing products (e.g. Name, Price…etc).
Cart: That holds the UIDs of the products currently chosen by each user.
Once user pays, an “Order” is created that hold the purchased products and some more info.
It should be possible to know how much the user actually payed for each product. Product hold product’s current price, which might change and be different from the actual price payed by user for this product. Thus, Order can not hold the products (and rely on their prices right now).
Therefore, there is an additional record: SinglePurchasedProduct. It will the product and the actual price payed by user for that product.
My question is:
Once user pays, how can I copy all his products from Cart and create SinglePurchasedProduct for each of them (with the price that was payed for each)?
How can I add all these newly created SinglePurchasedProduct items and add them to the Order I create?
@yoelk.yurak Yes this approach is much better and more performant.
The only adjustment I would make is the total price on cart; there’s no need for that field and for updating anytime the cart is updated; I’d just calculate it on front end when needed and then add the final total price value to the order dataset.
Thanks for the reply @Bruno_Duarte and @code-escapee .
Indeed, as you propose, my Cart now holds many lines ( that each line holds product, quantity, product’s category, owner…etc). I just simplified it for the sake of the question.
However, it is still not clear how do I:
Once user pays, how can I copy all his products from Cart and create SinglePurchasedProduct for each of them (with the price that was payed for each)?
How can I add all these newly created SinglePurchasedProduct items and add them to the Order I create?
But if you want to do it in another tricky way but less technical, you can create a singlepurchasedproduct whenever an item is added to the cart. Then, when the cart is paid for, you can calculate the line totals for each singlepurchaseditem and add it to an invoice (it is doable since there is a make-changes-to-a-list-of-things action). See my example running below:
In my example, a cart item is connected to a single purchased item whenever it is added to the cart and removed whenever the cart item is removed. So, at the end, there is always one-to-one list of cart items in the single purchased items list.
Then, when cart is paid, I am simply copying the amounts set in the cart to the single purchased items and calculate the line total etc. Of course, I am doing a search, but you should create a better data relation in here to find the actual cart item.
Finally, when the invoice is generated, all happens is get the list of single purchased items and you can print or calculate the invoice total. These can be done together with the previous step: