www/apps/resources/app/commerce-modules/loyalty/links-to-other-modules/page.mdx
import { CodeTabs, CodeTab, Table, Prerequisites } from "docs-ui"
export const metadata = {
title: Links between Loyalty Module and Other Modules,
}
This document showcases the module links defined between the Loyalty Module and other Commerce Modules.
<Prerequisites items={[ { text: "Loyalty Plugin Installed", link: "../page.mdx#1-install-the-loyalty-plugin", } ]} />
The Loyalty Module has the following links to other modules:
<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) in [Cart Module](../../cart/page.mdx) </Table.Cell> <Table.Cell> `GiftCard` in [Loyalty Module](../page.mdx) </Table.Cell> <Table.Cell> Stored - many-to-many </Table.Cell> <Table.Cell> [Learn more](#cart-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx) </Table.Cell> <Table.Cell> `GiftCard` in [Loyalty Module](../page.mdx) </Table.Cell> <Table.Cell> Stored - many-to-many </Table.Cell> <Table.Cell> [Learn more](#order-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> `GiftCard` in [Loyalty Module](../page.mdx) </Table.Cell> <Table.Cell> [OrderLineItem](/references/order/models/OrderLineItem) in [Order Module](../../order/page.mdx) </Table.Cell> <Table.Cell> Read-only </Table.Cell> <Table.Cell> [Learn more](#gift-card-and-order-line-item) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> `GiftCard` in [Loyalty Module](../page.mdx) </Table.Cell> <Table.Cell> `StoreCreditAccount` in [Store Credit Module](../../store-credit/page.mdx) </Table.Cell> <Table.Cell> Stored - many-to-one </Table.Cell> <Table.Cell> [Learn more](#store-credit-module) </Table.Cell> </Table.Row> </Table.Body> </Table>When a customer applies a gift card during checkout, Medusa defines a link between the Cart and GiftCard data models.
This allows you to retrieve the gift cards applied on a cart.
To retrieve the gift cards of a cart with Query, pass gift_cards.* in fields:
const { data: carts } = await query.graph({
entity: "cart",
fields: [
"gift_cards.*",
],
})
// carts[0].gift_cards
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: carts } = useQueryGraphStep({
entity: "cart",
fields: [
"gift_cards.*",
],
})
// carts[0].gift_cards
To manage the gift cards 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",
},
loyalty: {
gift_card_id: "gcard_123",
},
})
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.CART]: {
cart_id: "cart_123",
},
loyalty: {
gift_card_id: "gcard_123",
},
})
When a customer places an order that includes gift cards, Medusa defines a link between the Order and GiftCard data models.
This allows you to retrieve the gift cards applied on an order.
To retrieve the gift cards of an order with Query, pass gift_cards.* in fields:
const { data: orders } = await query.graph({
entity: "order",
fields: [
"gift_cards.*",
],
})
// orders[0].gift_cards
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: orders } = useQueryGraphStep({
entity: "order",
fields: [
"gift_cards.*",
],
})
// orders[0].gift_cards
To manage the gift cards of an order, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.ORDER]: {
order_id: "order_123",
},
loyalty: {
gift_card_id: "gcard_123",
},
})
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.ORDER]: {
order_id: "order_123",
},
loyalty: {
gift_card_id: "gcard_123",
},
})
Medusa defines a read-only link between the GiftCard data model and the Order Module's OrderLineItem data model. This means you can retrieve the order line item where the gift card was purchased, but you don't manage the links in a pivot table in the database. The line item of a gift card is determined by the line_item_id property of the GiftCard data model.
To retrieve the order line item of a gift card with Query, pass line_item.* in fields:
const { data: giftCards } = await query.graph({
entity: "gift_card",
fields: [
"line_item.*",
],
})
// giftCards[0].line_item
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: giftCards } = useQueryGraphStep({
entity: "gift_card",
fields: [
"line_item.*",
],
})
// giftCards[0].line_item
Medusa defines a link between the GiftCard and StoreCreditAccount from the Store Credit Module. This link is created when a gift card is created (alongside its store credit account) or when a gift card is redeemed.
This allows you to retrieve the store credit account associated with a gift card.
To retrieve the store credit account of a gift card with Query, pass store_credit_account.* in fields:
const { data: giftCards } = await query.graph({
entity: "gift_card",
fields: [
"store_credit_account.*",
],
})
// giftCards[0].store_credit_account
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: giftCards } = useQueryGraphStep({
entity: "gift_card",
fields: [
"store_credit_account.*",
],
})
// giftCards[0].store_credit_account
To manage the store credit account of a gift card, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">// ...
await link.create({
loyalty: {
gift_card_id: "gcard_123",
},
store_credit: {
store_credit_account_id: "sc_acc_123",
},
})
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
loyalty: {
gift_card_id: "gcard_123",
},
store_credit: {
store_credit_account_id: "sc_acc_123",
},
})