www/apps/resources/app/commerce-modules/cart/links-to-other-modules/page.mdx
import { CodeTabs, CodeTab, Table } from "docs-ui"
export const metadata = {
title: Links between Cart Module and Other Modules,
}
This document showcases the module links defined between the Cart Module and other Commerce Modules.
The Cart Module has the following links to other modules:
<Note title="Tip">Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
</Note> <Table> <Table.Header> <Table.Row> <Table.HeaderCell> First Data Model </Table.HeaderCell> <Table.HeaderCell> Second Data Model </Table.HeaderCell> <Table.HeaderCell> Type </Table.HeaderCell> <Table.HeaderCell> Description </Table.HeaderCell> </Table.Row> </Table.Header> <Table.Body> <Table.Row> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> [Customer](/references/customer/models/Customer) in [Customer Module](../../customer/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#customer-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [ShippingMethod](/references/cart/models/ShippingMethod) </Table.Cell> <Table.Cell> [ShippingOption](/references/fulfillment/models/ShippingOption) in [Fulfillment Module](../../fulfillment/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#fulfillment-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx) </Table.Cell> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> Stored - one-to-one </Table.Cell> <Table.Cell> [Learn more](#order-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> [PaymentCollection](/references/payment/models/PaymentCollection) in [Payment Module](../../payment/page.mdx) </Table.Cell> <Table.Cell> Stored - one-to-one </Table.Cell> <Table.Cell> [Learn more](#payment-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [LineItem](/references/cart/models/LineItem) </Table.Cell> <Table.Cell> [Product](/references/product/models/Product) in [Product Module](../../product/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#product-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [LineItem](/references/cart/models/LineItem) </Table.Cell> <Table.Cell> [ProductVariant](/references/product/models/ProductVariant) in [Product Module](../../product/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#product-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> [Promotion](/references/promotion/models/Promotion) in [Promotion Module](../../promotion/page.mdx) </Table.Cell> <Table.Cell> Stored - many-to-many </Table.Cell> <Table.Cell> [Learn more](#promotion-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> [Region](/references/region/models/Region) in [Region Module](../../region/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#region-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Cart](/references/cart/models/Cart) </Table.Cell> <Table.Cell> [SalesChannel](/references/sales-channel/models/SalesChannel) in [Sales Channel Module](../../sales-channel/page.mdx) </Table.Cell> <Table.Cell> Read-only - has one </Table.Cell> <Table.Cell> [Learn more](#sales-channel-module) </Table.Cell> </Table.Row> </Table.Body> </Table>Medusa defines a read-only link between the Cart data model and the Customer Module's Customer data model. This means you can retrieve the details of a cart's customer, but you don't manage the links in a pivot table in the database. The customer of a cart is determined by the customer_id property of the Cart data model.
To retrieve the customer of a cart with Query, pass customer.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"customer.*",
],
})
// carts[0].customer
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"customer.*",
],
})
// carts[0].customer
Medusa defines a read-only link between the ShippingMethod data model and the Fulfillment Module's ShippingOption data model. This means you can retrieve the details of a shipping method's shipping option, but you don't manage the links in a pivot table in the database. The shipping option of a shipping method is determined by the shipping_option_id property of the ShippingMethod data model.
This link allows you to retrieve the shipping option that a shipping method was created from.
<Note>This read-only link was added in Medusa v2.10.0
</Note>To retrieve the shipping option of a shipping method with Query, pass shipping_option.* in fields:
const { data: shippingMethods } = await query.graph({
entity: "shipping_method",
fields: [
"shipping_option.*",
],
})
// shippingMethods[0].shipping_option
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: shippingMethods } = useQueryGraphStep({
entity: "shipping_method",
fields: [
"shipping_option.*",
],
})
// shippingMethods[0].shipping_option
The Order Module provides order-management features.
Medusa defines a link between the Cart and Order data models. The cart is linked to the order created once the cart is completed.
To retrieve the order of a cart with Query, pass order.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"order.*",
],
})
// carts[0].order
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"order.*",
],
})
// carts[0].order
To manage the order of a cart, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.ORDER]: {
order_id: "order_123",
},
})
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.ORDER]: {
order_id: "order_123",
},
})
The Payment Module handles payment processing and management.
Medusa defines a link between the Cart and PaymentCollection data models. A cart has a payment collection which holds all the authorized payment sessions and payments made related to the cart.
To retrieve the payment collection of a cart with Query, pass payment_collection.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"payment_collection.*",
],
})
// carts[0].payment_collection
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"payment_collection.*",
],
})
// carts[0].payment_collection
To manage the payment collection of a cart, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.PAYMENT]: {
payment_collection_id: "paycol_123",
},
})
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.PAYMENT]: {
payment_collection_id: "paycol_123",
},
})
Medusa defines read-only links between:
LineItem data model and the Product Module's Product data model. This means you can retrieve the details of a line item's product, but you don't manage the links in a pivot table in the database. The product of a line item is determined by the product_id property of the LineItem data model.LineItem data model and the Product Module's ProductVariant data model. This means you can retrieve the details of a line item's variant, but you don't manage the links in a pivot table in the database. The variant of a line item is determined by the variant_id property of the LineItem data model.To retrieve the variant of a line item with Query, pass variant.* in fields:
To retrieve the product, pass product.* in fields.
const { data: lineItems } = await query.graph({
entity: "line_item",
fields: [
"variant.*",
],
})
// lineItems.variant
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: lineItems } = useQueryGraphStep({
entity: "line_item",
fields: [
"variant.*",
],
})
// lineItems.variant
The Promotion Module provides discount features.
Medusa defines a link between the Cart and Promotion data models. This indicates the promotions applied on a cart.
Medusa also defines a read-only link between the LineItemAdjustment and Promotion data models. This means you can retrieve the details of the promotion applied on a line item, but you don't manage the links in a pivot table in the database. The promotion of a line item is determined by the promotion_id property of the LineItemAdjustment data model.
To retrieve the promotions of a cart with Query, pass promotions.* in fields:
To retrieve the promotion of a line item adjustment, pass promotion.* in fields.
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"promotions.*",
],
})
// carts[0].promotions
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"promotions.*",
],
})
// carts[0].promotions
To manage the promotions of a cart, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.PROMOTION]: {
promotion_id: "promo_123",
},
})
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.CART]: {
cart_id: "cart_123",
},
[Modules.PROMOTION]: {
promotion_id: "promo_123",
},
})
Medusa defines a read-only link between the Cart data model and the Region Module's Region data model. This means you can retrieve the details of a cart's region, but you don't manage the links in a pivot table in the database. The region of a cart is determined by the region_id property of the Cart data model.
To retrieve the region of a cart with Query, pass region.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"region.*",
],
})
// carts[0].region
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"region.*",
],
})
// carts[0].region
Medusa defines a read-only link between the Cart data model and the Sales Channel Module's SalesChannel data model. This means you can retrieve the details of a cart's sales channel, but you don't manage the links in a pivot table in the database. The sales channel of a cart is determined by the sales_channel_id property of the Cart data model.
To retrieve the sales channel of a cart with Query, pass sales_channel.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"sales_channel.*",
],
})
// carts[0].sales_channel
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"sales_channel.*",
],
})
// carts[0].sales_channel