docs: improved commerce modules [5/5] (#9592)

- Improve remaining commerce modules
- Other: add a note about using methods of the modules' main services.
This commit is contained in:
Shahed Nasser
2024-10-16 10:25:21 +00:00
committed by GitHub
parent 6e856d3156
commit eed88c95ec
42 changed files with 594 additions and 582 deletions
@@ -8,21 +8,26 @@ export const metadata = {
In this guide, youll find common examples of how you can use the Tax Module in your application.
<Note>
You should only use the Tax Module's main service when implementing complex customizations. For common cases, check out [available workflows instead](../../../medusa-workflows-reference/page.mdx).
</Note>
## Create a Tax Region
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -75,16 +80,15 @@ export async function POST(request: Request) {
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -121,16 +125,15 @@ export async function GET(request: Request) {
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -199,16 +202,15 @@ export async function POST(request: Request) {
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -245,16 +247,15 @@ export async function GET(request: Request) {
<CodeTabs groupId="app-type">
<CodeTab label="Medusa API Router" value="medusa">
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -14,7 +14,7 @@ In this document, you'll learn about the options of the Tax Module.
## providers
The `providers` option is an array of either tax module providers, tax plugins, or path to a file that defines a tax provider.
The `providers` option is an array of either tax module providers or path to a file that defines a tax provider.
When the Medusa application starts, these providers are registered and can be used to retrieve tax lines.
@@ -6,7 +6,7 @@ export const metadata = {
# {metadata.title}
The Tax Module is the `@medusajs/medusa/tax` NPM package that provides tax-related features in your Medusa and Node.js applications.
The Tax Module provides tax-related features in your Medusa and Node.js applications.
## How to Use Tax Module's Service
@@ -15,18 +15,33 @@ You can use the Tax Module's main service by resolving from the Medusa container
For example:
<CodeTabs groupId="resource-type">
<CodeTab label="Workflow Step" value="workflow-step">
```ts title="src/workflows/hello-world/step1.ts"
import { createStep } from "@medusajs/framework/workflows-sdk"
import { Modules } from "@medusajs/framework/utils"
const step1 = createStep("step-1", async (_, { container }) => {
const taxModuleService = container.resolve(
Modules.TAX
)
const taxRegions = await taxModuleService.listTaxRegions()
})
```
</CodeTab>
<CodeTab label="API Route" value="api-route">
```ts title="src/api/store/custom/route.ts"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts title="src/api/store/custom/route.ts"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { Modules } from "@medusajs/framework/utils"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
const taxModuleService = req.scope.resolve(
Modules.TAX
)
@@ -39,35 +54,17 @@ export async function GET(
</CodeTab>
<CodeTab label="Subscriber" value="subscribers">
```ts title="src/subscribers/custom-handler.ts"
import { SubscriberArgs } from "@medusajs/framework"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
```ts title="src/subscribers/custom-handler.ts"
import { SubscriberArgs } from "@medusajs/framework"
import { Modules } from "@medusajs/framework/utils"
export default async function subscriberHandler({ container }: SubscriberArgs) {
const taxModuleService: ITaxModuleService = container.resolve(
const taxModuleService = container.resolve(
Modules.TAX
)
const taxRegions = await taxModuleService.listTaxRegions()
}
```
</CodeTab>
<CodeTab label="Workflow Step" value="workflow-step">
```ts title="src/workflows/hello-world/step1.ts"
import { createStep } from "@medusajs/framework/workflows-sdk"
import { ITaxModuleService } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
const step1 = createStep("step-1", async (_, { container }) => {
const taxModuleService: ITaxModuleService = container.resolve(
Modules.TAX
)
const taxRegions = await taxModuleService.listTaxRegions()
})
```
</CodeTab>
@@ -116,7 +113,7 @@ const taxRates = await taxModuleService.createTaxRates([
### Retrieve Cart's Tax Lines
Calculate and retrieve the tax lines of a cart's line items and shipping methods with tax providers. Use different tax providers for each region to handle tax-line retrieval differently.
Calculate and retrieve the tax lines of a cart's line items and shipping methods with tax providers.
```ts
const taxLines = await taxModuleService.getTaxLines(
@@ -141,6 +138,8 @@ const taxLines = await taxModuleService.getTaxLines(
)
```
You can use different tax providers for each region to handle tax-line retrieval differently.
---
## Configure Tax Module
@@ -10,7 +10,7 @@ In this document, youll learn how tax lines are calculated and what a tax pro
Tax lines are calculated and retrieved using the [getTaxLines method of the Tax Modules main service](/references/tax/getTaxLines). It accepts an array of line items and shipping methods, and the context of the calculation.
It returns the tax lines that are used to adjust the carts tax and grand totals.
For example:
```ts
const taxLines = await taxModuleService.getTaxLines(
@@ -37,7 +37,28 @@ const taxLines = await taxModuleService.getTaxLines(
The context object is used to determine which tax regions and rates to use in the calculation. It includes properties related to the address and customer.
The example above returns the tax lines based on the tax region for the United States.
The example above retrieves the tax lines based on the tax region for the United States.
The method returns tax lines for the line item and shipping methods. For example:
```json
[
{
"line_item_id": "cali_123",
"rate_id": "txr_1",
"rate": 10,
"code": "XXX",
"name": "Tax Rate 1"
},
{
"shipping_line_id": "casm_123",
"rate_id": "txr_2",
"rate": 5,
"code": "YYY",
"name": "Tax Rate 2"
}
]
```
---
@@ -22,7 +22,9 @@ Then, when tax rates are retrieved for a taxable item in the child region, both
## Override Tax Rates with Rules
You can create tax rates that override the default for specific conditions or rules. For example, the default tax rate is 10%, but for products of type “Shirt” is %15.
You can create tax rates that override the default for specific conditions or rules.
For example, you can have a default tax rate is 10%, but for products of type “Shirt” is %15.
A tax region can have multiple tax rates, and each tax rate can have multiple tax rules. The [TaxRateRule data model](/references/tax/models/TaxRateRule) represents a tax rates rule.
@@ -8,7 +8,7 @@ In this document, youll learn about tax regions and how to use them with the
## What is a Tax Region?
A tax region, represented by the [TaxRegion data model](/references/tax/models/TaxRegion), stores settings related to a region that your store serves.
A tax region, represented by the [TaxRegion data model](/references/tax/models/TaxRegion), stores tax settings related to a region that your store serves.
Tax regions can inherit settings and rules from a parent tax region.