chore: reorganize docs apps (#7228)

* reorganize docs apps

* add README

* fix directory

* add condition for old docs
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-05-03 17:36:38 +03:00
1 parent 224ebb2154
commit 4fe28f5a95
6187 files changed
+601447 -598226

No files matched your search

@@ -0,0 +1,141 @@
export const metadata = {
title: `Promotion Actions`,
}
# {metadata.title}
In this document, you’ll learn about promotion actions and how they’re computed and used.
## computeActions Method
The `IPromotionModuleService` has a `computeActions` method that returns an array of actions to perform on a cart when one or more promotions are applied.
<Note>
Refer to [the method’s reference](/references/promotion/computeActions) for full details on this method’s parameters and return type.
</Note>
Actions inform you what adjustment must be made to a cart item or shipping method. Each action is an object having the `action` field indicating the type of action.
---
## Action Types
### `addItemAdjustment` Action
The `addItemAdjustment` action indicates that an adjustment must be made to an item. For example, removing $5 off its amount.
This action has the following format:
```ts
export interface AddItemAdjustmentAction {
action: "addItemAdjustment"
item_id: string
amount: number
code: string
description?: string
}
```
When the Medusa application receives this action type, it creates a new record of the `LineItemAdjustment` data model in the Cart Module.
<Note>
Refer to [this reference](/references/promotion/interfaces/promotion.AddItemAdjustmentAction) for details on the object’s properties.
</Note>
### `removeItemAdjustment` Action
The `removeItemAdjustment` action indicates that an adjustment must be removed from a line item. For example, remove the $5 discount.
When the Medusa application uses the `computeActions` method, it passes any previous item adjustments in the `items` property of the second parameter.
This action has the following format:
```ts
export interface RemoveItemAdjustmentAction {
action: "removeItemAdjustment"
adjustment_id: string
description?: string
code: string
}
```
When the Medusa application receives this action type, it removes the `LineItemAdjustment` with the specified ID in the `adjustment_id` property.
<Note>
Refer to [this reference](/references/promotion/interfaces/promotion.RemoveItemAdjustmentAction) for details on the object’s properties.
</Note>
### `addShippingMethodAdjustment` Action
The `addShippingMethodAdjustment` action indicates that an adjustment must be made on a shipping method. For example, make the shipping method free.
This action has the following format:
```ts
export interface AddShippingMethodAdjustment {
action: "addShippingMethodAdjustment"
shipping_method_id: string
amount: number
code: string
description?: string
}
```
When the Medusa application receives this action type, it creates a new record of the `ShippingMethodAdjustment` data model in the Cart Module.
<Note>
Refer to [this reference](/references/promotion/interfaces/promotion.AddShippingMethodAdjustment) for details on the object’s properties.
</Note>
### `removeShippingMethodAdjustment` Action
The `removeShippingMethodAdjustment` action indicates that an adjustment must be removed from a shipping method. For example, remove the free shipping discount.
When the Medusa application uses the `computeActions` method, it passes any previous shipping method adjustments in the `shipping_methods` property of the second parameter.
This action has the following format:
```ts
export interface RemoveShippingMethodAdjustment {
action: "removeShippingMethodAdjustment"
adjustment_id: string
code: string
}
```
When the Medusa application receives this action type, it removes the `ShippingMethodAdjustment` with the specified ID in the `adjustment_id` property.
<Note>
Refer to [this reference](/references/promotion/interfaces/promotion.RemoveShippingMethodAdjustment) for details on the object’s properties.
</Note>
### `campaignBudgetExceeded` Action
When the `campaignBudgetExceeded` action is returned, the promotions within a campaign can no longer be used as the campaign budget has been exceeded.
This action has the following format:
```ts
export interface CampaignBudgetExceededAction {
action: "campaignBudgetExceeded"
code: string
}
```
The Medusa Application considers this an error, so it doesn’t apply the promotions on the cart.
<Note>
Refer to [this reference](/references/promotion/interfaces/promotion.CampaignBudgetExceededAction) for details on the object’s properties.
</Note>
@@ -0,0 +1,202 @@
import { Table } from "docs-ui"
export const metadata = {
title: `Promotion Concepts`,
}
# {metadata.title}
In this document, you’ll learn about the concepts in the Promotion Module.
## Promotion
A promotion, represented by the `Promotion` data model, represents a discount applied on cart items, shipping methods, or entire orders.
A promotion has two types:
- `standard`: A standard promotion with rules.
- `buyget`: “A buy X get Y” promotion with rules.
<Table>
<Table.Header>
<Table.Row>
<Table.HeaderCell>
`standard` Promotion Examples
</Table.HeaderCell>
<Table.HeaderCell>
`buyget` Promotion Examples
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>
A coupon code that gives customers 10% off their entire order.
</Table.Cell>
<Table.Cell>
Buy two shirts and get another for free.
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
A coupon code that gives customers $15 off any shirt in their order.
</Table.Cell>
<Table.Cell>
Buy two shirts and get 10% off the entire order.
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
A discount applied automatically for VIP customers that removes 10% off their shipping method’s amount.
</Table.Cell>
<Table.Cell>
Spend $100 and get free shipping.
</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
### PromotionRule
A promotion can be restricted by a set of rules, each rule is represented by the `PromotionRule` data model. For example, you can create a promotion that only customers of the `VIP` customer group can use it.
![A diagram showcasing the relation between Promotion and PromotionRule](https://res.cloudinary.com/dza7lstvk/image/upload/v1709833196/Medusa%20Resources/promotion-promotion-rule_msbx0w.jpg)
A `PromotionRule`'s `field` field indicates the field's name to which this rule is applied. For example, `customer_group_id`. Its value is stored in the `PromotionRuleValue` data model. So, a rule can have multiple values.
When testing whether a promotion can be applied to a cart, the rule's `field` field and its values are tested on the cart itself. For example, the cart's customer must be part of the customer group(s) indicated in the promotion rule's value.
### Flexible Rules
The `PromotionRule`'s `operator` field adds more flexibility to the rule’s condition rather than simple equality (`eq`).
For example, to restrict the promotion to only `VIP` and `B2B` customer groups:
- Add a `PromotionRule` with its `field` field set to `customer_group_id` and `operator` field to `in`.
- Add two `PromotionRuleValue` associated with the rule: one with the value `VIP` and the other `B2B`.
![A diagram showcasing the relation between PromotionRule and PromotionRuleValue when a rule has multiple values](https://res.cloudinary.com/dza7lstvk/image/upload/v1709897383/Medusa%20Resources/promotion-promotion-rule-multiple_hctpmt.jpg)
In this case, a customer’s group must be in the `VIP` and `B2B` set of values to use the promotion.
---
## Application Method
The `ApplicationMethod` data model defines how a promotion is applied:
<Table>
<Table.Header>
<Table.Row>
<Table.HeaderCell>
field
</Table.HeaderCell>
<Table.HeaderCell>
Purpose
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>
`type`
</Table.Cell>
<Table.Cell>
Does the promotion discount a fixed amount or a percentage?
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
`target_type`
</Table.Cell>
<Table.Cell>
Is the promotion applied on a cart item, shipping method, or the entire order?
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>
`allocation`
</Table.Cell>
<Table.Cell>
Is the discounted amount applied on each item or split between the applicable items?
</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
### Target Promotion Rules
When the promotion is applied to a cart item or a shipping method, you can restrict which items/shipping methods the promotion is applied to.
The `ApplicationMethod` data model has a collection of `PromotionRule` records to restrict which items or shipping methods the promotion applies to. The `target_rules` field represents this relation.
![A diagram showcasing the target_rules relation between the ApplicationMethod and PromotionRule data models](https://res.cloudinary.com/dza7lstvk/image/upload/v1709898273/Medusa%20Resources/application-method-target-rules_hqaymz.jpg)
In this example, the promotion is only applied on products in the cart having the SKU `SHIRT`.
### Buy Promotion Rules
When the promotion’s type is `buyget`, you must specify the “buy X” condition. For example, a cart must have two shirts before the promotion can be applied.
The application method has a collection of `PromotionRule` items to define the “buy X” rule. The `buy_rules` field represents this relation.
![A diagram showcasing the buy_rules relation between the ApplicationMethod and PromotionRule data models](https://res.cloudinary.com/dza7lstvk/image/upload/v1709898453/Medusa%20Resources/application-method-buy-rules_djjuhw.jpg)
In this example, the cart must have two products with the SKU `SHIRT` for the promotion to be applied.
---
## Campaign
A `Campaign` combines promotions under the same conditions, such as start and end dates.
![A diagram showcasing the relation between the Campaign and Promotion data models](https://res.cloudinary.com/dza7lstvk/image/upload/v1709899225/Medusa%20Resources/campagin-promotion_hh3qsi.jpg)
### Campaign Limits
Each campaign has a budget represented by the `CampaignBudget` data model. The budget limits how many times the promotion can be used.
There are two types of budgets:
- `spend`: An amount that, when crossed, the promotion becomes unusable. For example, if the amount limit is set to `$100`, and the total amount of usage of this promotion crosses that threshold, the promotion can no longer be applied.
- `usage`: The number of times that a promotion can be used. For example, if the usage limit is set to `10`, the promotion can be used only 10 times by customers. After that, it can no longer be applied.
![A diagram showcasing the relation between the Campaign and CampaignBudget data models](https://res.cloudinary.com/dza7lstvk/image/upload/v1709899463/Medusa%20Resources/campagin-budget_rvqlmi.jpg)
---
## Full Data Models Diagram
![A diagram showcasing all data models and the relations between them](https://res.cloudinary.com/dza7lstvk/image/upload/v1709900277/Medusa%20Resources/promotion-architecture_an8mzy.jpg)
@@ -0,0 +1,277 @@
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Examples of the Promotion Module`,
}
# {metadata.title}
In this document, you’ll find common examples of how you can use the Promotion Module in your application.
## Create a Promotion
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
})
res.json({ promotion })
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializePromotionModule,
} from "@medusajs/promotion"
export async function POST(request: Request) {
const promotionModuleService =
await initializePromotionModule()
const body = await request.json()
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
})
return NextResponse.json({ promotion })
}
```
</CodeTab>
</CodeTabs>
---
## Create a Campaign
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)
const campaign = await promotionModuleService.createCampaigns(
{
name: "Summer Discounts",
campaign_identifier: "G-123445",
starts_at: new Date("2024-05-02"),
ends_at: new Date("2024-07-20"),
promotions: [
{
id: "promo_123",
},
],
}
)
res.json({ campaign })
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializePromotionModule,
} from "@medusajs/promotion"
export async function POST(request: Request) {
const promotionModuleService =
await initializePromotionModule()
const body = await request.json()
const campaign = await promotionModuleService.createCampaigns(
{
name: "Summer Discounts",
campaign_identifier: "G-123445",
starts_at: new Date("2024-05-02"),
ends_at: new Date("2024-07-20"),
promotions: [
{
id: "promo_123",
},
],
}
)
return NextResponse.json({ campaign })
}
```
</CodeTab>
</CodeTabs>
---
## Create a Promotion with Flexible Rules
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
rules: [
{
field: "customer_group_id",
operator: "eq",
values: ["VIP"],
},
],
})
res.json({ promotion })
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializePromotionModule,
} from "@medusajs/promotion"
export async function POST(request: Request) {
const promotionModuleService =
await initializePromotionModule()
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
rules: [
{
field: "customer_group_id",
operator: "eq",
values: ["VIP"],
},
],
})
return NextResponse.json({ promotion })
}
```
</CodeTab>
</CodeTabs>
---
## List Promotions
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)
res.json({
promotions: await promotionModuleService.list(),
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializePromotionModule,
} from "@medusajs/promotion"
export async function POST(request: Request) {
const promotionModuleService =
await initializePromotionModule()
return NextResponse.json({
promotions: await promotionModuleService.list(),
})
}
```
</CodeTab>
</CodeTabs>
---
## More Examples
The [module interface reference](/references/promotion) provides a reference to all the methods available for use with examples for each.
@@ -0,0 +1,155 @@
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Promotion Module`,
}
# {metadata.title}
The Promotion Module is the `@medusajs/promotion` NPM package that provides promotion-related features in your Medusa and Node.js applications.
## Features
### Discount Functionalities
A promotion discounts an amount or percentage of a cart's items, shipping methods, or the entire order.
The Promotion Module allows you to store and manage promotions.
```ts
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
})
```
### Flexible Promotion Rules
A promotion has rules that restricts when it's applied. For example, you can create a promotion that's only applied to VIP customers.
```ts
const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
rules: [
{
field: "customer_group_id",
operator: "eq",
values: ["VIP"],
},
],
})
```
### Campaign Management
A campaign combines promotions under the same conditions, such as start and end dates.
A campaign can also have an identifier for tracking purposes, such as tracking through tools like Google Analytics.
```ts
const campaign = await promotionModuleService.createCampaigns(
{
name: "Summer Discounts",
campaign_identifier: "G-123445",
starts_at: new Date("2024-05-02"),
ends_at: new Date("2024-07-20"),
promotions: [
{
id: "promo_123",
},
],
}
)
```
---
## Configure Promotion Module
After installing the `@medusajs/promotion` package in your Medusa application, add it to the `modules` object in `medusa-config.js`:
```js title="medusa-config.js"
const modules = {
// ...
promotion: {
resolve: "@medusajs/promotion",
},
}
```
---
## How to Use the Promotion Module's Service
You can use the Promotion Module's main service by resolving from the Medusa container the resource `ModuleRegistrationName.PROMOTION` imported from `@medusajs/modules-sdk`.
For example:
<CodeTabs groupId="resource-type">
<CodeTab label="API Route" value="api-route">
```ts title="src/api/store/custom/route.ts"
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)
res.json({
promotions: await promotionModuleService.list(),
})
}
```
</CodeTab>
<CodeTab label="Subscriber" value="subscribers">
```ts title="src/subscribers/custom-handler.ts"
import { SubscriberArgs } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export default async function subscriberHandler({
container,
}: SubscriberArgs) {
const promotionModuleService: IPromotionModuleService =
container.resolve(ModuleRegistrationName.PROMOTION)
const promotions = await promotionModuleService.list()
}
```
</CodeTab>
<CodeTab label="Workflow Step" value="workflow-step">
```ts title="src/workflows/hello-world/step1.ts"
import { createStep } from "@medusajs/workflows-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
const step1 = createStep("step-1", async (_, context) => {
const promotionModuleService: IPromotionModuleService =
context.container.resolve(ModuleRegistrationName.PROMOTION)
const promotions = await promotionModuleService.list()
})
```
</CodeTab>
</CodeTabs>
@@ -0,0 +1,17 @@
export const metadata = {
title: `Relations between Promotion Module and Other Modules`,
}
# {metadata.title}
When Commerce Modules are used together in a Medusa application, the Medusa application handles building the relations between these modules.
This document showcases the relation between the Promotion Module and other Commerce Modules.
## Cart Module
When the Cart and Promotion modules are used together, the Medusa application links the `Promotion` data model to the `Cart`, `LineItemAdjustment`, and `ShippingMethodAdjustment` data models of the Cart Module.
![A diagram showcasing an example of how resources from the Promotion and Cart modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1709905821/Medusa%20Resources/promotion-cart_z5qklr.jpg)
A promotion is linked to a cart when a promotion is applied on the cart. Adjustments on items and shipping methods, represented by the `LineItemAdjustment` and `ShippingMethodAdjustment` data models, are linked to the promotion as well.