3.2.10. Commerce Modules

In this chapter, you'll learn about Medusa's commerce modules.

What is a Commerce Module?#

Commerce modules are built-in modules of Medusa that provide core commerce logic specific to domains like Products, Orders, Customers, Fulfillment, and much more.

Medusa's commerce modules are used to form Medusa's default workflows and APIs. For example, when you call the add to cart endpoint. the add to cart workflow runs which uses the Product Module to check if the product exists, the Inventory Module to ensure the product is available in the inventory, and the Cart Module to finally add the product to the cart.

TipYou'll find the details and steps of the add-to-cart workflow in this workflow reference

The core commerce logic contained in Commerce Modules is also available directly when you are building customizations. This granular access to commerce functionality is unique and expands what's possible to build with Medusa drastically.

List of Medusa's Commerce Modules#

Refer to this reference for a full list of commerce modules in Medusa.


Use Commerce Modules in Custom Flows#

Similar to your custom modules, the Medusa application registers a commerce module's service in the container. So, you can resolve it in your custom flows. This is useful as you build unique requirements extending core commerce features.

For example, consider you have a workflow (a special function that performs a task in a series of steps with rollback mechanism) that needs a step to retrieve the total number of products. You can create a step in the workflow that resolves the Product Module's service from the container to use its methods:

Code
1import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"2
3export const countProductsStep = createStep(4  "count-products",5  async ({ }, { container }) => {6    const productModuleService = container.resolve("product")7
8    const [,count] = await productModuleService.listAndCountProducts()9
10    return new StepResponse(count)11  }12)

Your workflow can use services of both custom and commerce modules, supporting you in building custom flows without having to re-build core commerce features.

Was this chapter helpful?
Edit this page