Skip to content

Customise Core

Add your company creation and purchasing rules through Core’s extensions. Run Core workflows in your backend, or use Medusa subscribers to synchronise changes with another system. Your application must authorise the caller before invoking backend workflows.

Choose a customisation

NeedUse
Control company creation or buyer activationvalidateRegistration for Store company creation; validateBuyerGrant for Admin buyer activation.
Refuse a purchase with your own rulevalidatePurchase extension. It can only refuse.
Add a price or promotion attributecommercialContext, with declared commercial_keys. It also runs for B2C: return company-specific benefits only when input.commerce_type === "b2b".
Create an order or a draft order from the backendcreateCompanyOrderWorkflow, createCompanyDraftOrderWorkflow from @mantajs/core/workflows.
Protect your own Store routerequireCompany, requireCart, readCompanyOrder helpers.
Synchronise company or buyer changes with another systemA Medusa subscriber for the corresponding Core event. Use event_id to avoid processing the same event twice.

Register an extension

Application extensions are optional. Register your validators and commercial context contributions with the extensions option. Start with the Core extensions reference.

Validators can refuse an operation by throwing an error. commercialContext supplies attributes to Medusa’s pricing and promotion rules; it does not grant access. Validators may run more than once, so keep them free of side effects. Direct backend CRUD calls are privileged and do not run validators.

Example: disable company creation from the storefront

For a portal where your team creates companies in the Admin, this validator refuses company creation from the storefront.

src/b2b/manual-registration.ts
import type { B2BExtension } from "@mantajs/core/extensions"

export const manualRegistration: B2BExtension = {
  id: "app.manual-registration",
  validateRegistration() {
    throw new Error("Company registration is managed by our team")
  },
}

Import the validator in medusa-config.ts and add it to Core’s options.extensions array in your existing plugins configuration:

medusa-config.ts
import { manualRegistration } from "./src/b2b/manual-registration"

// Inside defineConfig({ ... }), keep your other plugins and options.
plugins: [
  {
    resolve: "@mantajs/core",
    options: { extensions: [manualRegistration] },
  },
],

Create company orders and drafts

Use createCompanyOrderWorkflow or createCompanyDraftOrderWorkflow from @mantajs/core/workflows in your backend. For an existing cart, prepareCompanyCartDraftWorkflow can copy it to a draft order or transfer it. The transfer mode blocks further buyer changes (state transferred). It does not transfer an existing order.

An interrupted preparation can enter needs_recovery; there is no automatic repair. See the Core extensions reference for inputs and recovery behaviour.

Authorise backend calls

Authenticate and authorise the caller before invoking backend workflows. Pass the customer ID from authentication to requireCompany and requireCart, never an ID supplied in the request body. Calling Query or a generated CRUD service does not check the caller’s access.

Apply access rules to lists, detail views, writes and exports. readCompanyOrder without a customer ID is reserved for an already authorised backend caller.

Events and subscribers

Create a Medusa subscriber in src/subscribers to synchronise company and buyer changes with your CRM or another service. Core also emits b2b.order.placed after linking an order to its company. Use event_id to avoid processing the same event twice.

An event can be delivered more than once, and delivery is not guaranteed after a crash. For synchronisation that must recover missed changes, reconcile your external system with Core’s stored data.

The Core extensions reference lists the events, payloads, transaction boundaries and available Core extensions.