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 :

Fixed Amount + (Total items in cart - 1) * Additional price per item

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

class CustomFulfillmentService extends AbstractFulfillmentProviderService {
    static identifier = "custom-fulfillment"

    async calculatePrice(
        optionData: CalculateShippingOptionPriceDTO["optionData"],
        data: CalculateShippingOptionPriceDTO["data"],
        context: CalculateShippingOptionPriceDTO["context"]
    ): Promise<CalculatedShippingOptionPrice> {
        // 1️⃣ Get the total number of items in the cart
        const totalItems = context.items.reduce((sum, item) => sum + Number(item.quantity), 0)

        // 2️⃣ If there are no items, return 0
        if (!totalItems) {
            return {
                calculated_amount: 0,
                is_calculated_price_tax_inclusive: true,
            }
        }

        // 3️⃣ Define the base shipping cost and the additional item shipping cost
        const BASE_SHIPPING_COST = 5 // $5 in cents
        const ADDITIONAL_ITEM_SHIPPING_COST = 2 // $2 in cents

        // 4️⃣ Calculate the total price
        const totalPrice = BASE_SHIPPING_COST + (totalItems - 1) * ADDITIONAL_ITEM_SHIPPING_COST

        return {
            calculated_amount: totalPrice,
            is_calculated_price_tax_inclusive: true,
        }
    }
    
    // ...
}
Don’t forget to enable your FulfillmentProvider in the Admin UI and setup everything related to it inside your regions.

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.