Files
medusa-store/www/apps/resources/app/commerce-modules/customer/examples/page.mdx
Shahed Nasser 0462cc5acf docs: updates to use DML and other changes (#7834)
- Change existing data model guides and add new ones for DML
- Change module's docs around service factory + remove guides that are now necessary
- Hide/remove all mentions of module relationships, or label them as coming soon.
- Change all data model creation snippets to use DML
- use `property` instead of `field` when referring to a data model's properties.
- Fix all snippets in commerce module guides to use new method suffix (no more main model methods)
- Rework recipes, removing/hiding a lot of sections as a lot of recipes are incomplete with the current state of DML.


### Other changes

- Highlight fixes in some guides
- Remove feature flags guide
- Fix code block styles when there are no line numbers.

### Upcoming changes in other PRs

- Re-generate commerce module references (for the updates in the method names)
- Ensure that the data model references are generated correctly for models using DML.
- (probably at a very later point) revisit recipes
2024-06-26 07:55:59 +00:00

237 lines
5.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: `Examples of the Customer Module`,
}
# {metadata.title}
In this guide, youll find common examples of how you can use the Customer Module in your application.
## Create a Customer
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
const customer = await customerModuleService.createCustomers({
first_name: "Peter",
last_name: "Hayes",
email: "peter.hayes@example.com",
})
res.json({
customer,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
const customer = await customerModuleService.createCustomers({
first_name: "Peter",
last_name: "Hayes",
email: "peter.hayes@example.com",
})
return NextResponse.json({
customer,
})
}
```
</CodeTab>
</CodeTabs>
---
## Create a Customer Group
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
const customerGroup =
await customerModuleService.createCustomerGroups({
name: "VIP",
})
res.json({
customer_group: customerGroup,
})
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
const customerGroup =
await customerModuleService.createCustomerGroups({
name: "VIP",
})
return NextResponse.json({
customer_group: customerGroup,
})
}
```
</CodeTab>
</CodeTabs>
---
## Add a Customer to a Group
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
await customerModuleService.addCustomerToGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
res.status(200)
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
await customerModuleService.addCustomerToGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
return NextResponse.json({}, { status: 200 })
}
```
</CodeTab>
</CodeTabs>
---
## Remove a Customer from a Group
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const customerModuleService: ICustomerModuleService =
request.scope.resolve(ModuleRegistrationName.CUSTOMER)
await customerModuleService.removeCustomerFromGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
res.status(200)
}
```
</CodeTab>
<CodeTab label="Next.js App Router" value="nextjs">
```ts
import { NextResponse } from "next/server"
// eslint-disable-next-line prettier/prettier
import {
initialize as initializeCustomerModule,
} from "@medusajs/customer"
export async function POST(request: Request) {
const customerModuleService = await initializeCustomerModule()
await customerModuleService.removeCustomerFromGroup({
customer_id: "cus_123",
customer_group_id: "cusgroup_123",
})
return NextResponse.json({}, { status: 200 })
}
```
</CodeTab>
</CodeTabs>
---
## More Examples
The [Customer Module's main service reference](/references/customer) provides a reference to all the methods available for use with examples for each.