General description of shipping in Medusa
In Medusa a shipping option represents a way for the customer to receive their order. A traditional shipping option is through a carrier such as FedEx, GLS, DHL, etc. where a package is picked up at a warehouse by the carrier and then transported to the customer's address. Different shipping options may be available in a store; for example, a store may have both a standard shipping option and an express shipping option. The shipping options can have different prices, e.g. express shipping is usually more expensive than standard shipping. Furthermore, the carrier that a shipping option is processed through may differ, e.g. express shipping could be through GLS, while standard shipping is done through DHL. Finally, an order may be processed by different fulfillment providers, e.g. some products may come from one warehouse, and others from a different warehouse. Finally finally, some regions may fulfill orders in different ways than other regions, e.g. orders shipping to the US should be shipped from the Canadian warehouse while products shipping to Europe should be shipped from the Swedish warehouse.
To accommodate all the complexities of shipping in an easy manner Medusa's shipping workflow starts with the fulfillment providers. starts with Shipping Profiles. A Shipping Profile contains a list of products and a list of shipping options that can fulfill any of the products on the list. Each of the shipping options defined in the list will have a fulfillment provider, a region and a price. The fulfillment provider takes care of handling where the order is shipped from and can also provide additional functionality like calculate prices, etc.
A store can have any number of fulfillment providers usually integrated via plugins. For example, a store may have a ShipHero plugin to take care of orders that should be fulfilled with ShipHero. When the store operator creates a shipping option for a region they will have to provide the option with one of ShipHero's shipping options, say Express Shipping with DHL. This tells Medusa that orders that have this shipping option should be fulfilled by ShipHero and the integration will make sure to transfer the order to ShipHero and select Express Shipping with DHL.
All fulfillment provider services have a method called getShippingOptions
, which returns an array of shipping options available via the fulfillment provider. The store operator can choose to create shipping options for all of the fulfillment provider's shipping options or only a couple of them.
When creating shipping options in Medusa the store operator gives the option a name and a price. Prices can be set in different ways, for example, if the fulfillment provider service can calculate rates for a shipping option the price can be variable depending on the cart's contents. Furthermore, a shipping option may only be available for some products or for orders greater than a certain amount.
ShippingOptionService
The shipping option service takes care of CRUD operations on the shipping options available in a store, and is responsible for communication between fulfillment provider services and the store operator.
Additionally, the shipping option service must be able to:
- Determine if a cart qualifies for a given shipping option
- Fetch the price of a shipping option depending on a cart's status (i.e. items, shipping destination, total). This may happen by asking the fulfillment provider for the shipping price, or by looking at the dimensions of the items in the cart.
- Validate a shipping method: for example, when a customer picks a shipping option we want to validate that the method is available for the cart. Additionally, if the method takes some sort of input (e.g. a parcel shop id) the shipping option service has to ask the fulfillment provider if the input is valid.
Shipping Option Schema
name: the name of the shipping option (string)
provider_id: the id of the provider that fulfills this shipping method (string)
data: data that the provider needs to be able to fulfill the order (object)
price: {
type: the type can be (flat_rate or calculated)
amount: the price of the shipping option if the price type is flat_rate (float)
}
requirements: an array of requirements that must be satisfied for the shipping method to be available
RequirementsSchema (this is only an initial thought it will make sense to make this more advanced)
type: minimum_subtotal | maximum_subtotal | includes_variant_id | excludes_variant_id
value: a number or id
Signatures
retrieve(optionId)
create(shippingOption)
update(optionId, shippingOption)
delete(optionId)
checkAvailability(optionId, cart)
fetchCartOptions(cart)
- returns all available shipping options for the cart
validate(optionId, data, cart)