www/apps/resources/app/commerce-modules/order/promotion-adjustments/page.mdx
export const metadata = {
title: Promotions Adjustments in Orders,
}
In this document, you’ll learn how a promotion is applied to an order’s items and shipping methods using adjustment lines.
An adjustment line indicates a change to a line item or a shipping method’s amount. It’s used to apply promotions or discounts on an order.
The OrderLineItemAdjustment data model represents changes on a line item, and the OrderShippingMethodAdjustment data model represents changes on a shipping method.
The amount property of the adjustment line indicates the amount to be discounted from the original amount.
The ID of the applied promotion is stored in the promotion_id property of the adjustment line.
The OrderLineItem data model has an is_discountable property 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.
When using the Order and Promotion modules together, 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.
</Note>import {
ComputeActionAdjustmentLine,
ComputeActionItemLine,
ComputeActionShippingLine,
// ...
} from "@medusajs/framework/types"
// ...
// retrieve the order
const order = await orderModuleService.retrieveOrder("ord_123", {
relations: [
"items.item.adjustments",
"shipping_methods.shipping_method.adjustments",
],
})
// retrieve the line item adjustments
const lineItemAdjustments: ComputeActionItemLine[] = []
order.items.forEach((item) => {
const filteredAdjustments = item.adjustments?.filter(
(adjustment) => adjustment.code !== undefined
) as unknown as ComputeActionAdjustmentLine[]
if (filteredAdjustments.length) {
lineItemAdjustments.push({
...item,
...item.detail,
adjustments: filteredAdjustments,
})
}
})
//retrieve shipping method adjustments
const shippingMethodAdjustments: ComputeActionShippingLine[] =
[]
order.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,
// TODO infer from cart or region
currency_code: "usd",
}
)
The computeActions method accepts the existing adjustments of line items and shipping methods to compute the actions accurately.
Then, use the returned addItemAdjustment and addShippingMethodAdjustment actions to set the order’s line items and the shipping method’s adjustments.
import {
AddItemAdjustmentAction,
AddShippingMethodAdjustment,
// ...
} from "@medusajs/framework/types"
// ...
await orderModuleService.setOrderLineItemAdjustments(
order.id,
actions.filter(
(action) => action.action === "addItemAdjustment"
) as AddItemAdjustmentAction[]
)
await orderModuleService.setOrderShippingMethodAdjustments(
order.id,
actions.filter(
(action) =>
action.action === "addShippingMethodAdjustment"
) as AddShippingMethodAdjustment[]
)