Dynamic Shipping Prices
Implementing dynamic shipping prices in Medusa
How do I manage dynamic delivery rates based on X or Y conditions?
This question was asked on Discord by a community member.
Join the Medusa Community
Join the Medusa Community on Discord to get help, share your knowledge, and stay up to date with the latest news and updates.
The problem
The problem is this: how can we increase the delivery price based on X or Y conditions? In the case of our community member, he wanted to increase the price based on a fixed amount, depending on the total number of items in the cart.
For example:
- Fixed amount of $5
- $2 per item added to the cart
In the case where we only have one item in the cart, the price would be $5.
If we have 2 items, the price would be $7 :
The solution
What we need to keep in mind here is that we’ll need a new fulfillment provider, one that we can customize as we wish and inject our own logic.
The official documentation covers the creation of a FulfillmentProvider very well “here”
Once your FulfillmentProvider is created, the function we’re going to be interested in modifying is calculatePrice
, it’s inside that the magic will happen
The returned calculated_amount
is the price that will be displayed to the customer and used for any further calculations inside Medusa.
Example in action
Below is a video showing you the complete process + an example on the Storefront.
Conclusion
This is a simple example of how to implement dynamic shipping prices in Medusa and how to use the calculatePrice
function.
You can find the complete code on “GitHub”
Join the Medusa Community
Join the Medusa Community on Discord to get help, share your knowledge, and stay up to date with the latest news and updates.