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,45 @@
export const metadata = {
title: `Cart Concepts`,
}
# {metadata.title}
In this document, you’ll get an overview of the main concepts of a cart.
## Shipping and Billing Addresses
A cart has a shipping and billing address. Both of these addresses are represented by the `Address` data model.
![A diagram showcasing the relation between the Cart and Address data models](https://res.cloudinary.com/dza7lstvk/image/upload/v1711532392/Medusa%20Resources/cart-addresses_ls6qmv.jpg)
---
## Line Items
A line item, represented by the `LineItem` data model, is a product added to the cart. A cart has multiple line items.
A line item stores some of the product’s fields, such as the `product_title` and `product_description`. It also stores data related to the item’s quantity and price.
<Note>
A product can be from the [Product Module](../../product/page.mdx) but can also be a custom item used only in this cart.
</Note>
---
## Shipping Methods
A shipping method, represented by the `ShippingMethod` data model, is the method used to fulfill the items in the cart after the order is placed. A cart can have more than one shipping method.
If the shipping method is created from a shipping option, typically available through the Fulfillment Module, its ID is stored in the `shipping_option_id`.
A shipping method can also be a custom method associated with this cart only.
### data Field
When fulfilling the order after its placed, you may use a third-party fulfillment provider that requires additional custom data to be passed along from the checkout process.
The `ShippingMethod` data model has a `data` field. It's an object used to store custom data relevant later for fulfillment.
In the Medusa application, it's the `data` is passed to the Fulfillment Module when fulfilling items.
@@ -0,0 +1,485 @@
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Examples of the Cart Module`,
}
# {metadata.title}
In this guide, you’ll find common examples of how you can use the Cart Module in your application.
## Create a Cart
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const cart = await cartModuleService.create({
currency_code: "usd",
shipping_address: {
address_1: "1512 Barataria Blvd",
country_code: "us",
},
items: [
{
title: "Shirt",
unit_price: 1000,
quantity: 1,
},
],
})
res.json({ cart })
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function POST(request: Request) {
const cartModuleService = await initializeCartModule()
const cart = await cartModuleService.create({
currency_code: "usd",
shipping_address: {
address_1: "1512 Barataria Blvd",
country_code: "us",
},
items: [
{
title: "Shirt",
unit_price: 1000,
quantity: 1,
},
],
})
return NextResponse.json({
cart,
})
}
```
</CodeTab>
</CodeTabs>
---
## Retrieve Cart by ID
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const cart = await cartModuleService.retrieve("cart_123")
res.json({ cart })
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function GET(request: Request) {
const cartModuleService = await initializeCartModule()
const cart = await cartModuleService.retrieve("cart_123")
return NextResponse.json({
cart,
})
}
```
</CodeTab>
</CodeTabs>
---
## Add Item to Cart
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const lineItem = await cartModuleService.addLineItems({
cart_id: "cart_123",
title: "Shirt",
quantity: 2,
unit_price: 5000,
})
res.json({
line_item: lineItem,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function POST(
request: Request
) {
const cartModuleService = await initializeCartModule()
const lineItem = await cartModuleService.addLineItems({
cart_id: "cart_123",
title: "Shirt",
quantity: 2,
unit_price: 5000,
})
return NextResponse.json({
line_item: lineItem,
})
}
```
</CodeTab>
</CodeTabs>
---
## Add Shipping Method to Cart
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const shippingMethod =
await cartModuleService.addShippingMethods({
cart_id: "cart_123",
name: "Custom shipping",
amount: 1000,
})
res.json({
shipping_method: shippingMethod,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function POST(
request: Request
) {
const cartModuleService = await initializeCartModule()
const shippingMethod =
await cartModuleService.addShippingMethods({
cart_id: "cart_123",
name: "Custom shipping",
amount: 1000,
})
return NextResponse.json({
shipping_method: shippingMethod,
})
}
```
</CodeTab>
</CodeTabs>
---
## Add Item Adjustment Line
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const itemAdjustment =
await cartModuleService.addLineItemAdjustments({
item_id: "cali_123",
amount: 500,
code: "50%OFF",
})
res.json({
adjustment: itemAdjustment,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function POST(
request: Request
) {
const cartModuleService = await initializeCartModule()
const itemAdjustment =
await cartModuleService.addLineItemAdjustments({
item_id: "cali_123",
amount: 500,
code: "50%OFF",
})
return NextResponse.json({
adjustment: itemAdjustment,
})
}
```
</CodeTab>
</CodeTabs>
---
## Add Shipping Method Adjustment Line
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
const shippingMethodAdjustment =
await cartModuleService.addShippingMethodAdjustments({
shipping_method_id: "casm_123",
amount: 500,
code: "FREESHIPPING",
})
res.json({
adjustment: shippingMethodAdjustment,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function POST(
request: Request
) {
const cartModuleService = await initializeCartModule()
const shippingMethodAdjustment =
await cartModuleService.addShippingMethodAdjustments({
shipping_method_id: "casm_123",
amount: 500,
code: "FREESHIPPING",
})
return NextResponse.json({
adjustment: shippingMethodAdjustment,
})
}
```
</CodeTab>
</CodeTabs>
---
## Remove Line Item from Cart
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
await cartModuleService.deleteLineItems(["cali_123"])
res.status(200)
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCartModule,
} from "@medusajs/cart"
export async function DELETE(
request: Request
) {
const cartModuleService = await initializeCartModule()
await cartModuleService.deleteLineItems(["cali_123"])
}
```
</CodeTab>
</CodeTabs>
---
## Remove Shipping Method from Cart
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
await cartModuleService.deleteLineItems(["cali_123"])
res.status(200)
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
await cartModuleService.deleteShippingMethods(["casm_123"])
res.status(200)
}
```
</CodeTab>
</CodeTabs>
---
## More Examples
The [module interface reference](/references/cart) provides a reference to all the methods available for use with examples for each.
@@ -0,0 +1,147 @@
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Cart Module`,
}
# {metadata.title}
The Cart Module is the `@medusajs/cart` NPM package that provides cart-related features in your Medusa and Node.js applications.
---
## Features
### Cart Management
Store and manage carts, including their addresses, line items, shipping methods, and more.
```ts
const cart = await cartModuleService.create({
currency_code: "usd",
shipping_address: {
address_1: "1512 Barataria Blvd",
country_code: "us",
},
items: [
{
title: "Shirt",
unit_price: 1000,
quantity: 1,
},
],
})
```
### Apply Promotions
Apply promotions or discounts to line items and shipping methods by adding adjustment lines that are factored into their subtotals.
```ts
const lineAdjustments =
await cartModuleService.addLineItemAdjustments({
item_id: "cali_123",
code: "50OFF",
amount: 500,
})
const shippingAdjustments =
await cartModuleService.addShippingMethodAdjustments({
shipping_method_id: "casm_123",
code: "FREESHIPPING",
amount: 1000,
})
```
### Cart Context and Scoping
A cart is scoped to a sales channel, region, and a customer.
When used with their respective modules and other commerce modules, you benefit from features like:
- Checking product availability in a sales channel.
- Retrieving pricing per region.
- Applying promotions based on the customer's group.
---
## Configure Cart Module
After installing the `@medusajs/cart` package in your Medusa application, add it to the `modules` object in `medusa-config.js`:
```js title="medusa-config.js"
const modules = {
// ...
cart: {
resolve: "@medusajs/cart",
},
}
```
---
## How to Use Cart Module's Service
You can use the Cart Module's main service by resolving from the Medusa container the resource `ModuleRegistrationName.CART` 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 { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { res } from "express"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const cartModuleService: ICartModuleService =
req.scope.resolve(ModuleRegistrationName.CART)
res.json({
carts: await cartModuleService.list(),
})
}
```
</CodeTab>
<CodeTab label="Subscriber" value="subscribers">
```ts title="src/subscribers/custom-handler.ts"
import { SubscriberArgs } from "@medusajs/medusa"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export default async function subscriberHandler({
container,
}: SubscriberArgs) {
const cartModuleService: ICartModuleService =
container.resolve(ModuleRegistrationName.CART)
const carts = await cartModuleService.list()
}
```
</CodeTab>
<CodeTab label="Workflow Step" value="workflow-step">
```ts title="src/workflows/hello-world/step1.ts"
import { createStep } from "@medusajs/workflows-sdk"
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
const step1 = createStep("step-1", async (_, context) => {
const cartModuleService: ICartModuleService =
context.container.resolve(
ModuleRegistrationName.CART
)
const carts = await cartModuleService.list()
})
```
</CodeTab>
</CodeTabs>
@@ -0,0 +1,132 @@
---
sidebar_label: "Promotions Adjustments"
---
export const metadata = {
title: `Promotions Adjustments in Carts`,
}
# {metadata.title}
In this document, you’ll learn how a promotion is applied to a cart’s line items and shipping methods using adjustment lines.
## What are Adjustment Lines?
An adjustment line indicates a change to an item or a shipping method’s amount. It’s used to apply promotions or discounts on a cart.
The `LineItemAdjustment` data model represents adjustment lines for a line item, and the `ShippingMethodAdjustment` data model represents adjustment lines for a shipping method.
![A diagram showcasing the relations between other data models and adjustment line models](https://res.cloudinary.com/dza7lstvk/image/upload/v1711534248/Medusa%20Resources/cart-adjustments_k4sttb.jpg)
The `amount` field of the adjustment line indicates the amount to be discounted from the original amount. Also, the ID of the applied promotion can be stored in the `promotion_id` field of the adjustment line.
---
## Promotion Actions
When using the Cart and Promotion modules together, such as in the Medusa application, use the `computeActions` method of the Promotion Module’s main service. It retrieves the actions of line items and shipping methods.
<Note>
Learn more about actions in the [Promotion Module’s documentation](../../promotion/actions/page.mdx).
</Note>
```ts
import {
ComputeActionAdjustmentLine,
ComputeActionItemLine,
ComputeActionShippingLine,
// ...
} from "@medusajs/types"
// retrieve the cart
const cart = await cartModuleService.retrieve("cart_123", {
relations: [
"items.adjustments",
"shipping_methods.adjustments",
],
})
// retrieve line item adjustments
const lineItemAdjustments: ComputeActionItemLine[] = []
cart.items.forEach((item) => {
const filteredAdjustments = item.adjustments?.filter(
(adjustment) => adjustment.code !== undefined
) as unknown as ComputeActionAdjustmentLine[]
if (filteredAdjustments.length) {
lineItemAdjustments.push({
...item,
adjustments: filteredAdjustments,
})
}
})
// retrieve shipping method adjustments
const shippingMethodAdjustments: ComputeActionShippingLine[] =
[]
cart.shipping_methods.forEach((shippingMethod) => {
const filteredAdjustments =
shippingMethod.adjustments?.filter(
(adjustment) => adjustment.code !== undefined
) as unknown as ComputeActionAdjustmentLine[]
if (filteredAdjustments.length) {
shippingMethodAdjustments.push({
...shippingMethod,
adjustments: filteredAdjustments,
})
}
})
// compute actions
const actions = await promotionModuleService.computeActions(
["promo_123"],
{
items: lineItemAdjustments,
shipping_methods: shippingMethodAdjustments,
}
)
```
The `computeActions` method accepts the existing adjustments of line items and shipping methods to compute the actions accurately.
<Note>
Learn more about the `computeActions` method in [this reference](/references/promotion/computeActions).
</Note>
Then, use the returned `addItemAdjustment` and `addShippingMethodAdjustment` actions to set the cart’s line item and the shipping method’s adjustments.
```ts
import {
AddItemAdjustmentAction,
AddShippingMethodAdjustment,
// ...
} from "@medusajs/types"
// ...
await cartModuleService.setLineItemAdjustments(
cart.id,
actions.filter(
(action) => action.action === "addItemAdjustment"
) as AddItemAdjustmentAction[]
)
await cartModuleService.setShippingMethodAdjustments(
cart.id,
actions.filter(
(action) =>
action.action === "addShippingMethodAdjustment"
) as AddShippingMethodAdjustment[]
)
```
---
## Discountable Option
The `LineItem` data model has an `is_discountable` field that indicates whether promotions can be applied to the line item. It’s enabled by default.
When disabled, a promotion can’t be applied to a line item. In the context of the Promotion Module, the promotion isn’t applied to the line item even if it matches its rules.
@@ -0,0 +1,39 @@
export const metadata = {
title: `Relations between Cart 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 Cart Module and other Commerce Modules.
## Customer Module
A cart is scoped to the customer using it. The Medusa application forms a relation between the `Cart` and `Customer` data models.
![A diagram showcasing an example of how resources from the Cart and Customer modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711537276/Medusa%20Resources/cart-customer_pnjvuw.jpg)
---
## Payment Module
The Payment Module allows you to associate payments with a cart. The Medusa application forms a relation between the `Cart` and `PaymentCollection` data models.
![A diagram showcasing an example of how resources from the Cart and Payment modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711537849/Medusa%20Resources/cart-payment_ixziqm.jpg)
---
## Promotion Module
A promotion can be applied on line items and shipping methods of a cart. The Medusa application forms a relation between the `Cart` and `Promotion` data models.
![A diagram showcasing an example of how resources from the Cart and Promotion modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711538015/Medusa%20Resources/cart-promotion_kuh9vm.jpg)
---
## Sales Channel Module
A cart is scoped to a sales channel. The Medusa application forms a relation between the `Cart` and `SalesChannel` data models.
![A diagram showcasing an example of how resources from the Cart and Sales Channel modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711538159/Medusa%20Resources/cart-sales-channel_n2oi0v.jpg)
@@ -0,0 +1,79 @@
export const metadata = {
title: `Tax Lines in Cart Module`,
}
# {metadata.title}
In this document, you’ll learn about tax lines in a cart and how to retrieve tax lines with the Tax Module.
## What are Tax Lines?
A tax line indicates the tax rate of a line item or a shipping method. The `LineItemTaxLine` data model represents a line item’s tax line, and the `ShippingMethodTaxLine` data model represents a shipping method’s tax line.
![A diagram showcasing the relation between other data models and the tax line models](https://res.cloudinary.com/dza7lstvk/image/upload/v1711534431/Medusa%20Resources/cart-tax-lines_oheaq6.jpg)
---
## Tax Inclusivity
By default, the tax amount is calculated by taking the tax rate from the line item or shipping method’s amount, and then added to the item/method’s subtotal.
However, line items and shipping methods have an `is_tax_inclusive` field that, when enabled, indicates that the item or method’s price already includes taxes.
So, instead of calculating the tax rate and adding it to the item/method’s subtotal, it’s calculated as part of the subtotal.
<Note>
The following diagram is a simplified showcase of how a subtotal is calculated from the taxes perspective.
</Note>
![A diagram showing an example of calculating the subtotal of a line item using its taxes](https://res.cloudinary.com/dza7lstvk/image/upload/v1711535295/Medusa%20Resources/cart-tax-inclusive_shpr3t.jpg)
---
## Retrieving Tax Lines
When using the Cart and Tax modules together, such as in the Medusa application, you can use the `getTaxLines` method of the Tax Module’s main service. It retrieves the tax lines for a cart’s line items and shipping methods.
```ts
// retrieve the cart
const cart = await cartModuleService.retrieve("cart_123", {
relations: [
"items.tax_lines",
"shipping_methods.tax_lines",
"shipping_address",
],
})
// retrieve the tax lines
const taxLines = await taxModuleService.getTaxLines(
[
...(cart.items as TaxableItemDTO[]),
...(cart.shipping_methods as TaxableShippingDTO[]),
],
{
address: {
...cart.shipping_address,
country_code:
cart.shipping_address.country_code || "us",
},
}
)
```
Then, use the returned tax lines to set the line items and shipping methods’ tax lines:
```ts
// set line item tax lines
await cartModuleService.setLineItemTaxLines(
cart.id,
taxLines.filter((line) => "line_item_id" in line)
)
// set shipping method tax lines
await cartModuleService.setLineItemTaxLines(
cart.id,
taxLines.filter((line) => "shipping_line_id" in line)
)
```