chore: reorganize docs apps (#7228)

* reorganize docs apps

* add README

* fix directory

* add condition for old docs
This commit is contained in:
Shahed Nasser
2024-05-03 17:36:38 +03:00
committed by GitHub
parent 224ebb2154
commit 4fe28f5a95
6187 changed files with 601447 additions and 598226 deletions
@@ -0,0 +1,132 @@
---
sidebar_label: "Promotions Adjustments"
---
export const metadata = {
title: `Promotions Adjustments in Carts`,
}
# {metadata.title}
In this document, youll learn how a promotion is applied to a carts line items and shipping methods using adjustment lines.
## What are Adjustment Lines?
An adjustment line indicates a change to an item or a shipping methods amount. Its 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 Modules main service. It retrieves the actions of line items and shipping methods.
<Note>
Learn more about actions in the [Promotion Modules 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 carts line item and the shipping methods 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. Its enabled by default.
When disabled, a promotion cant be applied to a line item. In the context of the Promotion Module, the promotion isnt applied to the line item even if it matches its rules.