#How would I get the line items from an order/order id to create a fulfillment?
1 messages · Page 1 of 1 (latest)
Thanks, I thought it might be related (haha) to passing in relationships, thank you
Hi could you share the code of how to create the fulfillment automatically after the order is placed
Here's what I wrote:
constructor({ fulfillmentService, orderService, eventBusService }) {
this.orderService = orderService;
this.fulfillmentService = fulfillmentService;
eventBusService.subscribe("order.placed", this.handleOrder);
}
handleOrder = async (data) => {
const order = await this.orderService.retrieve(data.id, {
relations: ["items"],
});
const createdFulfillment = await this.orderService.createFulfillment(
data.id,
order.items
);
};
}
export default OrderFulfillmentSubscriber;
Awesome. thank you
hey, sorry I am new to backend, just wondering what fullfilment provider you are using? My fulfillment provider provides API endpoints to send a put request to create a fullfilment provider, and I am using createFulfillment function to do that. Not sure if I am doing correctly. And should I use one of the plugins such as BrightPearl, etc
I wrote a custom fulfillment provider. This createFulfillment method I showed above is on the order service, does your fulfillment provider integrate with Medusa directly? Or are you trying to fire off this put request on Medusa fulfillment creation
I created the fulfillment provider file in services folder, extends the FulfillmentService class, and in the createFulfillment method, I am trying to fire off the put request. I added the OrderFulfillmentSubscriber (your code) to the subscribers folder. So when the order is placed, the Put request will be sent. Am I doing that right...
That part I'm not sure of, I haven't gotten to that part, I have to do a similar task - I had an issue where my orders aren't getting a shipping method for some reason. Once I've got that resolved and I can get a successful fulfillment created, I will get back to you if you haven't figured it out first.
But I think that you're on the right track - that's what I'm thinking would work too
Awesome. Thank you for your help
Hi, just FYI, I was not able to get the fulfillmentItems in the createFulfillment method, so I went to the docs and used the AI, the items passed to "this.orderService.createFulfillment" should be in other format
handleOrder = async (data) => {
const order = await this.orderService.retrieve(data.id, {
relations: ['items'],
});
const fulfillmentItems: FulFillmentItemType[] = order.items.map((item) => ({
item_id: item.id,
quantity: item.quantity,
}));
await this.orderService.createFulfillment(data.id, fulfillmentItems);
};
Here is my updated code and it worked
Ok, thanks! Let me know all the rest of your progress too, as it's late here and I'm not really working right now, but will be in the morning my time