Skip to content

Core data model

Core adds companies, buyer memberships, company groups and addresses in the b2b_core module. Carts and orders remain native Medusa objects; CartContext and OrderContext link them to the company.

Overview

Data model
Customer (Medusa) ──< Buyer >── Company ──< CompanyAddress
                                   │
                                   └──< CompanyGroupMembership >── CompanyGroup

Cart (Medusa)  ── CartContext  ──> Company     one company per cart
Order (Medusa) ── OrderContext ──> Company     one company per order

A customer can belong to several companies. Each company has its own addresses and can belong to several company groups. A company cart or order is linked to exactly one company; personal purchases have no company context.

Objects

ObjectMain fields and rules
Companyname, unique slug, currency_code (default eur), status (active or suspended), metadata.
CompanyAddresskind (billing or shipping), label, first_name, last_name, address_1, address_2, city, postal_code, country_code, province, phone, is_default_shipping. At most one non-deleted billing address and one default shipping address per company.
Buyercompany_id, customer_id, active. One membership per company and customer. Revoke it by deactivating it; reactivate the same membership.
CompanyGroupname, description.
CompanyGroupMembershipcompany_id, company_group_id.
CartContextunique cart_id, company_id, creator’s customer_id, origin.
OrderContextunique order_id, company_id, customer_id, origin, placed_by_actor_type, placed_by_actor_id, snapshot.
CommerceSettingmode: mixed or b2b_only. Read by Dashboard.

Three technical objects complete the module: OrderPlacement (order provenance), CartHold (a purchase hold on a cart, owned by another module or by a Core cart-to-draft transfer) and CartDraftPreparation (preparing a draft order from a cart). All Core objects, like Medusa models, have an ID, timestamps and soft deletion.

Read them with Query:

FromAliasTo
Customercustomer.b2b_buyerstheir buyer memberships (Buyer)
Cartcart.b2b_contextits company context (CartContext)
Orderorder.b2b_contextits company context (OrderContext)

Data kept when an order is placed

  • A shared cart keeps its creator, even when a colleague picks it up.
  • The person who placed the order is in OrderContext.placed_by_actor_type and placed_by_actor_id. Order.customer_id can still point to the creator.
  • When your team creates an order for a company in the Admin, the order can belong to that company without a linked Medusa customer (Order.customer_id is empty). The Admin user placing it is recorded separately.
  • When the order is placed, a snapshot records the company’s identity (id, name, slug, currency), the addresses and the contact email. Later changes to the company don’t rewrite past orders.

Rules

  • Attaching a cart to the same company twice returns the existing link. Attaching it to another company is refused.
  • Suspending a company blocks its buyers’ access to its Store routes. Merchants can still view and manage its orders in the Admin.
  • Revoking a buyer’s membership keeps the history.
  • A company with orders can’t be deleted through the API.
  • Use Core workflows for writes. Direct CRUD service calls do not check the caller’s access or run the extension validators; reserve them for authorised backend code.

Core workflows and extensions