How to copy several items into a DB?

Hi all,

I am building a shop app and have

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:

  1. 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)?

  2. How can I add all these newly created SinglePurchasedProduct items and add them to the Order I create?

Thank you

I would approach that in a different way. What I would do is to create 3 data types:

  • Products (which have all the products info like name, price, stock, Vat categorie, etc);
  • Lines of the cart (product, quantity, price, vat amount, total price);
  • Cart ( List of Lines of cart, total price=Lines of the cartSUM) ;

So the logic is, when a user select an item, choose the quantity and click the button, you create cart line and then will be add to a cart.

Like this you can properly work the data, have acess only to the lines of the cart you want.

I only put the most common fields on the data types, but you can add all the ones that suits better your project.

1 Like

@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:

  1. 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)?

  2. How can I add all these newly created SinglePurchasedProduct items and add them to the Order I create?

You can create a list of things but it is tricky indeed. Explained here: Create list of things and attach to another list of things 1 by 1

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:

invoice

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:
image

And this is the data structure underneath all these:

3 Likes

Thank you very much @hergin for the detailed explanation and for for effort you put into building this demo!

Could you please share your app so that I can dig into the details of how exactly you made it and “play” with it?

Sure thing. Check the cart page please.

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.