Back to Medusa

{metadata.title}

www/apps/resources/app/commerce-modules/loyalty/page.mdx

2.14.25.4 KB
Original Source

import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = { title: Loyalty Module, }

{metadata.title}

In this section of the documentation, you will find resources to learn more about the Loyalty Module and how to use it in your application.

<Note>

The Loyalty Module is part of the Loyalty Plugin. It's compatible with Medusa v2.14.0+.

</Note> <Note title="Looking for no-code docs?">

Refer to the Medusa Admin User Guide to learn how to manage gift cards using the dashboard.

</Note>

Medusa has loyalty related features available through the Loyalty Module. A module is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Loyalty Module.

<Note>

Learn more about why modules are isolated in this documentation.

</Note>

Features


How to Use the Loyalty Module

1. Install the Loyalty Plugin

The Loyalty Module is part of the Loyalty Plugin. So, install the loyalty plugin:

bash
npm install @medusajs/loyalty-plugin

Then, add it to your medusa-config.js:

js
module.exports = defineConfig({
  // ... other configurations
  plugins: [
    {
      resolve: `@medusajs/loyalty-plugin`,
      options: {},
    },
    // ... other plugins
  ],
})

2. Run Migrations

After installing the plugin, run the migrations to create the necessary tables in the database:

bash
npx medusa db:migrate

3. Use Loyalty Features in Your Application

In your Medusa application, you build flows around Commerce Modules. A flow is built as a Workflow, which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.

You can build custom workflows and steps. You can also re-use the Loyalty Plugin's workflows and steps.

For example:

export const highlights = [ ["11", "loyalty", "Resolve the module in a step."] ]

ts
import { 
  createWorkflow, 
  WorkflowResponse,
  createStep,
  StepResponse,
} from "@medusajs/framework/workflows-sdk"

const createGiftCardStep = createStep(
  "create-gift-card",
  async ({}, { container }) => {
    const loyaltyModuleService = container.resolve("loyalty")

    const giftCard = await loyaltyModuleService.createGiftCards({
      code: "test-code",
      value: 50,
      currency_code: "usd",
      expires_at: null,
      reference_id: "order_123",
      reference: "order",
      line_item_id: "litem_123",
      customer_id: "customer_123",
    })

    return new StepResponse({ giftCard }, giftCard.id)
  },
  async (giftCardId, { container }) => {
    const loyaltyModuleService = container.resolve("loyalty")

    await loyaltyModuleService.deleteGiftCards([giftCardId])
  }
)

export const createGiftCardWorkflow = createWorkflow(
  "create-gift-card",
  () => {
    const { giftCard } = createGiftCardStep()

    return new WorkflowResponse({
      giftCard,
    })
  }
)

You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:

<CodeTabs group="resource-types"> <CodeTab label="API Route" value="api-route">
ts
import type {
  MedusaRequest,
  MedusaResponse,
} from "@medusajs/framework/http"
import { createGiftCardWorkflow } from "../../workflows/create-gift-card"

export async function GET(
  req: MedusaRequest,
  res: MedusaResponse
) {
  const { result } = await createGiftCardWorkflow(req.scope)
    .run()

  res.send(result)
}
</CodeTab> <CodeTab label="Subscriber" value="subscriber">
ts
import {
  type SubscriberConfig,
  type SubscriberArgs,
} from "@medusajs/framework"
import { createGiftCardWorkflow } from "../workflows/create-gift-card"

export default async function handleOrderCreated({
  event: { data },
  container,
}: SubscriberArgs<{ id: string }>) {
  const { result } = await createGiftCardWorkflow(container)
    .run()

  console.log(result)
}

export const config: SubscriberConfig = {
  event: "order.created",
}
</CodeTab> <CodeTab label="Scheduled Job" value="scheduled-job">
ts
import { MedusaContainer } from "@medusajs/framework/types"
import { createGiftCardWorkflow } from "../workflows/create-gift-card"

export default async function myCustomJob(
  container: MedusaContainer
) {
  const { result } = await createGiftCardWorkflow(container)
    .run()

  console.log(result)
}

export const config = {
  name: "run-once-a-day",
  schedule: `0 0 * * *`,
}
</CodeTab> </CodeTabs>

Learn more about workflows in this documentation.


<CommerceModuleSections />