--- products: - customer - order - payment - product - promotion - region - sales channel --- import { CodeTabs, CodeTab, Table } from "docs-ui" export const metadata = { title: `Links between Cart Module and Other Modules`, } # {metadata.title} This document showcases the module links defined between the Cart Module and other Commerce Modules. ## Summary The Cart Module has the following links to other modules: Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database. First Data Model Second Data Model Type Description [Cart](/references/cart/models/Cart) [Customer](/references/customer/models/Customer) in [Customer Module](../../customer/page.mdx) Read-only - has one [Learn more](#customer-module) [ShippingMethod](/references/cart/models/ShippingMethod) [ShippingOption](/references/fulfillment/models/ShippingOption) in [Fulfillment Module](../../fulfillment/page.mdx) Read-only - has one [Learn more](#fulfillment-module) [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx) [Cart](/references/cart/models/Cart) Stored - one-to-one [Learn more](#order-module) [Cart](/references/cart/models/Cart) [PaymentCollection](/references/payment/models/PaymentCollection) in [Payment Module](../../payment/page.mdx) Stored - one-to-one [Learn more](#payment-module) [LineItem](/references/cart/models/LineItem) [Product](/references/product/models/Product) in [Product Module](../../product/page.mdx) Read-only - has one [Learn more](#product-module) [LineItem](/references/cart/models/LineItem) [ProductVariant](/references/product/models/ProductVariant) in [Product Module](../../product/page.mdx) Read-only - has one [Learn more](#product-module) [Cart](/references/cart/models/Cart) [Promotion](/references/promotion/models/Promotion) in [Promotion Module](../../promotion/page.mdx) Stored - many-to-many [Learn more](#promotion-module) [Cart](/references/cart/models/Cart) [Region](/references/region/models/Region) in [Region Module](../../region/page.mdx) Read-only - has one [Learn more](#region-module) [Cart](/references/cart/models/Cart) [SalesChannel](/references/sales-channel/models/SalesChannel) in [Sales Channel Module](../../sales-channel/page.mdx) Read-only - has one [Learn more](#sales-channel-module)
--- ## Customer Module Medusa defines a read-only link between the `Cart` data model and the [Customer Module](../../customer/page.mdx)'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. ### Retrieve with Query To retrieve the customer of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `customer.*` in `fields`: ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "customer.*", ], }) // carts[0].customer ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "customer.*", ], }) // carts[0].customer ``` --- ## Fulfillment Module Medusa defines a read-only link between the `ShippingMethod` data model and the [Fulfillment Module](../../fulfillment/page.mdx)'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. This read-only link was added in [Medusa v2.10.0](https://github.com/medusajs/medusa/releases/tag/v2.10.0) ### Retrieve with Query To retrieve the shipping option of a shipping method with [Query](!docs!/learn/fundamentals/module-links/query), pass `shipping_option.*` in `fields`: ```ts const { data: shippingMethods } = await query.graph({ entity: "shipping_method", fields: [ "shipping_option.*", ], }) // shippingMethods[0].shipping_option ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: shippingMethods } = useQueryGraphStep({ entity: "shipping_method", fields: [ "shipping_option.*", ], }) // shippingMethods[0].shipping_option ``` ## Order Module The [Order Module](../../order/page.mdx) 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. ![A diagram showcasing an example of how data models from the Cart and Order modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1728375735/Medusa%20Resources/cart-order_ijwmfs.jpg) ### Retrieve with Query To retrieve the order of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `order.*` in `fields`: ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "order.*", ], }) // carts[0].order ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "order.*", ], }) // carts[0].order ``` ### Manage with Link To manage the order of a cart, use [Link](!docs!/learn/fundamentals/module-links/link): ```ts import { Modules } from "@medusajs/framework/utils" // ... await link.create({ [Modules.CART]: { cart_id: "cart_123", }, [Modules.ORDER]: { order_id: "order_123", }, }) ``` ```ts 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", }, }) ``` --- ## Payment Module The [Payment Module](../../payment/page.mdx) 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. ![A diagram showcasing an example of how data models from the Cart and Payment modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711537849/Medusa%20Resources/cart-payment_ixziqm.jpg) ### Retrieve with Query To retrieve the payment collection of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `payment_collection.*` in `fields`: ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "payment_collection.*", ], }) // carts[0].payment_collection ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "payment_collection.*", ], }) // carts[0].payment_collection ``` ### Manage with Link To manage the payment collection of a cart, use [Link](!docs!/learn/fundamentals/module-links/link): ```ts import { Modules } from "@medusajs/framework/utils" // ... await link.create({ [Modules.CART]: { cart_id: "cart_123", }, [Modules.PAYMENT]: { payment_collection_id: "paycol_123", }, }) ``` ```ts import { createRemoteLinkStep } from "@medusajs/medusa/core-flows" // ... createRemoteLinkStep({ [Modules.CART]: { cart_id: "cart_123", }, [Modules.PAYMENT]: { payment_collection_id: "paycol_123", }, }) ``` --- ## Product Module Medusa defines read-only links between: - the `LineItem` data model and the [Product Module](../../product/page.mdx)'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. - the `LineItem` data model and the [Product Module](../../product/page.mdx)'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. ### Retrieve with Query To retrieve the variant of a line item with [Query](!docs!/learn/fundamentals/module-links/query), pass `variant.*` in `fields`: To retrieve the product, pass `product.*` in `fields`. ```ts const { data: lineItems } = await query.graph({ entity: "line_item", fields: [ "variant.*", ], }) // lineItems.variant ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: lineItems } = useQueryGraphStep({ entity: "line_item", fields: [ "variant.*", ], }) // lineItems.variant ``` --- ## Promotion Module The [Promotion Module](../../promotion/page.mdx) provides discount features. Medusa defines a link between the `Cart` and `Promotion` data models. This indicates the promotions applied on a cart. ![A diagram showcasing an example of how data models from the Cart and Promotion modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711538015/Medusa%20Resources/cart-promotion_kuh9vm.jpg) 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. ### Retrieve with Query To retrieve the promotions of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `promotions.*` in `fields`: To retrieve the promotion of a line item adjustment, pass `promotion.*` in `fields`. ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "promotions.*", ], }) // carts[0].promotions ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "promotions.*", ], }) // carts[0].promotions ``` ### Manage with Link To manage the promotions of a cart, use [Link](!docs!/learn/fundamentals/module-links/link): ```ts import { Modules } from "@medusajs/framework/utils" // ... await link.create({ [Modules.CART]: { cart_id: "cart_123", }, [Modules.PROMOTION]: { promotion_id: "promo_123", }, }) ``` ```ts 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", }, }) ``` --- ## Region Module Medusa defines a read-only link between the `Cart` data model and the [Region Module](../../region/page.mdx)'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. ### Retrieve with Query To retrieve the region of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `region.*` in `fields`: ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "region.*", ], }) // carts[0].region ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "region.*", ], }) // carts[0].region ``` --- ## Sales Channel Module Medusa defines a read-only link between the `Cart` data model and the [Sales Channel Module](../../sales-channel/page.mdx)'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. ### Retrieve with Query To retrieve the sales channel of a cart with [Query](!docs!/learn/fundamentals/module-links/query), pass `sales_channel.*` in `fields`: ```ts const { data: carts } = await query.graph({ entity: "cart", fields: [ "sales_channel.*", ], }) // carts[0].sales_channel ``` ```ts import { useQueryGraphStep } from "@medusajs/medusa/core-flows" // ... const { data: carts } = useQueryGraphStep({ entity: "cart", fields: [ "sales_channel.*", ], }) // carts[0].sales_channel ```