Setting the file. One moment.
Skill 14 · Building With Medusa
Subchapter 14.12
reference/workflow-hooks.mdMarkdown3 KBView on GitHub
Workflow hooks let you inject custom logic into existing Medusa workflows without recreating them. Use them to extend core commerce flows.
Note: Hooks run in-band (synchronously within the workflow). If your task can run in the background, use a subscriber instead for better performance.
// src/workflows/hooks/product-created.ts
import { createProductsWorkflow } from "@medusajs/medusa/core-flows"
import { StepResponse } from "@medusajs/framework/workflows-sdk"
createProductsWorkflow.hooks.productsCreated(
// Hook handler
async ({ products, additional_data }, { container }) => {
if (!additional_data?.brand_id) {
return new StepResponse([], [])
}
const link = container.resolve("link")
// Link products to brand
const linkData = products.map((product) => ({
product: { product_id: product.id },
brand: { brand_id: additional_data.brand_id },
}))
await link.create(linkData)
return new StepResponse(linkData, linkData)
},
// Compensation (runs if workflow fails after this point)
async (linkData, { container }) => {
const link = container.resolve("link")
await link.dismiss(linkData)
}
)createProductsWorkflow.hooks.productsCreated - After products are createdcreateOrderWorkflow.hooks.orderCreated - After an order is createdupdateCartPromotionsWorkflow.hooks.setPromotionContext - Inject extra context into promotion computation (v2.15.0+)upsertTaxLinesWorkflow.hooks.setTaxLineContext, updateTaxLinesWorkflow.hooks.setTaxLineContext, updateOrderTaxLinesWorkflow.hooks.setTaxLineContext - Customize the context passed to tax calculation (v2.16.0+)Context hooks (setPromotionContext, setTaxLineContext) are the supported way to feed custom data into Medusa’s promotion and tax calculation instead of overriding the workflows:
// src/workflows/hooks/promotion.ts
import { StepResponse } from "@medusajs/framework/workflows-sdk"
import { updateCartPromotionsWorkflow } from "@medusajs/medusa/core-flows"
updateCartPromotionsWorkflow.hooks.setPromotionContext(
({ cart }, { container }) => {
if (cart.items.length >= 2) {
return new StepResponse({ company_id: "company_123" })
}
}
)Use workflow hooks when:
Use subscribers when: