Raw
1 ---
2 sidebar_label: "Promotions Adjustments"
3 products:
4 - promotion
5 ---
6
7 export const metadata = {
8 title: `Promotions Adjustments in Carts`,
9 }
10
11 # {metadata.title}
12
13 In this document, you’ll learn how a promotion is applied to a cart’s line items and shipping methods using adjustment lines.
14
15 ## What are Adjustment Lines?
16
17 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.
18
19 The [LineItemAdjustment](/references/cart/models/LineItemAdjustment) data model represents changes on a line item, and the [ShippingMethodAdjustment](/references/cart/models/ShippingMethodAdjustment) data model represents changes on a shipping method.
20
21 ![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)
22
23 The `amount` property of the adjustment line indicates the amount to be discounted from the original amount. Also, the ID of the applied promotion is stored in the `promotion_id` property of the adjustment line.
24
25 ---
26
27 ## Discountable Option
28
29 The [LineItem](/references/cart/models/LineItem) data model has an `is_discountable` property that indicates whether promotions can be applied to the line item. It’s enabled by default.
30
31 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.
32
33
34 ---
35
36 ## Promotion Actions
37
38 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](/references/promotion/computeActions). It retrieves the actions of line items and shipping methods.
39
40 <Note>
41
42 Learn more about actions in the [Promotion Module’s documentation](../../promotion/actions/page.mdx).
43
44 </Note>
45
46 For example:
47
48 ```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
49 import {
50 ComputeActionAdjustmentLine,
51 ComputeActionItemLine,
52 ComputeActionShippingLine,
53 // ...
54 } from "@medusajs/framework/types"
55
56 // retrieve the cart
57 const cart = await cartModuleService.retrieveCart("cart_123", {
58 relations: [
59 "items.adjustments",
60 "shipping_methods.adjustments",
61 ],
62 })
63
64 // retrieve line item adjustments
65 const lineItemAdjustments: ComputeActionItemLine[] = []
66 cart.items.forEach((item) => {
67 const filteredAdjustments = item.adjustments?.filter(
68 (adjustment) => adjustment.code !== undefined
69 ) as unknown as ComputeActionAdjustmentLine[]
70 if (filteredAdjustments.length) {
71 lineItemAdjustments.push({
72 ...item,
73 adjustments: filteredAdjustments,
74 })
75 }
76 })
77
78 // retrieve shipping method adjustments
79 const shippingMethodAdjustments: ComputeActionShippingLine[] =
80 []
81 cart.shipping_methods.forEach((shippingMethod) => {
82 const filteredAdjustments =
83 shippingMethod.adjustments?.filter(
84 (adjustment) => adjustment.code !== undefined
85 ) as unknown as ComputeActionAdjustmentLine[]
86 if (filteredAdjustments.length) {
87 shippingMethodAdjustments.push({
88 ...shippingMethod,
89 adjustments: filteredAdjustments,
90 })
91 }
92 })
93
94 // compute actions
95 const actions = await promotionModuleService.computeActions(
96 ["promo_123"],
97 {
98 items: lineItemAdjustments,
99 shipping_methods: shippingMethodAdjustments,
100 }
101 )
102 ```
103
104 The `computeActions` method accepts the existing adjustments of line items and shipping methods to compute the actions accurately.
105
106 Then, use the returned `addItemAdjustment` and `addShippingMethodAdjustment` actions to set the cart’s line item and the shipping method’s adjustments.
107
108 ```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
109 import {
110 AddItemAdjustmentAction,
111 AddShippingMethodAdjustment,
112 // ...
113 } from "@medusajs/framework/types"
114
115 // ...
116
117 await cartModuleService.setLineItemAdjustments(
118 cart.id,
119 actions.filter(
120 (action) => action.action === "addItemAdjustment"
121 ) as AddItemAdjustmentAction[]
122 )
123
124 await cartModuleService.setShippingMethodAdjustments(
125 cart.id,
126 actions.filter(
127 (action) =>
128 action.action === "addShippingMethodAdjustment"
129 ) as AddShippingMethodAdjustment[]
130 )
131 ```