I’m trying to create orders for an e-commerce application (later, I’ll implement Stripe). I already have Users, and Products content-types. Here is what I want to do, creating Orders content-type:
Do you have any idea on what I should use in Strapi ? I tried content-types, dynamic zones and components and I almost got this, but I couldn’t make it. I just need some help for the “details” part.
Orders content-type (User has many Orders, Order has many Order-Details):
Order-details content-type (Order-Detail has one Product):
Note: order_details.name is generated inside lifecycle, by concatenating product name & quantity so I could obtain this view inside Orders Content-type:
async beforeCreate(data, model) {
let product = await strapi.query('products').findOne({ id: data.product });
data.name = `${product.name} x ${data.quantity}`;
},
async beforeUpdate(data, model) {
let product = await strapi.query('products').findOne({ id: model.product });
model.name = `${product.name} x ${model.quantity}`;
},
This is a very clear solution !
So I tried this workaround, I have a question about it: to create an Order, do I need to create an OrderDetail first ??
I’m not sure about how to implement this, since I’ll create orders through a POST request on /orders. Should I do first a POST request on /order_details then ?
First, overwrite the order’s create() controller and define all that business logic there. Inside it you need to create the order-details first (because you need their IDs to refer them in the relation of order), then you create the order with a reference to order-details.
That way you will have a single controller that creates both: order-details and order. Now you can make a single POST request to /orders from your front-end.
There is something wrong. My client needs to be able, from the administrative panel, to check the orders her clients have paid so she can send them their product(s). Right now she has only the ids of OrderDetails in Orders in the admin panel. Should I use a component for this ?
Also when I think about it, I don’t need OrderDetails to be a Content-Type since I won’t do any request on it.
I just need a nested “details” (products and quantity for each one inside) in “orders”.
This is very basic normally, I don’t get why it’s that hard
Btw is it normal controller isn’t call (to check the body of Strapi’s requests for example) ? It only goes through my create(ctx) function while I use API requests, not directly Strapi. I would like to be able to have control on both (Strapi’s administrative panel and API requests). If it’s not the case, I guess I’ll have to duplicate the code ?
For those who would like to know, I created a simple repeatable component (as @sunnyson suggested) named “details” into my Order content-type and it worked pretty well !
This component has a “Order-Details” category, and inside there are:
quantity: Number
product: relation with Products: Order has one Product