Raw
1 ---
2 tags:
3 - name: product
4 label: "Variant Price Rules"
5 - name: fulfillment
6 label: "Shipping Option Price Rules"
7 - concept
8 products:
9 - product
10 - fulfillment
11 ---
12
13 import { CodeTabs, CodeTab } from "docs-ui"
14
15 export const metadata = {
16 title: `Price Tiers and Rules`,
17 }
18
19 # {metadata.title}
20
21 In this Pricing Module guide, you'll learn about tired prices, price rules for price sets and price lists, and how to add rules to a price.
22
23 <Note title="Looking for no-code docs?">
24
25 You can manage prices and tiers using the Medusa Admin:
26
27 - [Edit shipping option prices with conditions](!user-guide!/settings/locations-and-shipping/locations#fixed-and-conditional-shipping-option-prices).
28 - [Add price lists to set prices with conditions for product variants](!user-guide!/price-lists).
29
30 </Note>
31
32 ## Tiered Pricing
33
34 Each price, represented by the [Price data model](/references/pricing/models/Price), has two optional properties that can be used to create tiered prices:
35
36 - `min_quantity`: The minimum quantity that must be in the cart for the price to be applied.
37 - `max_quantity`: The maximum quantity that can be in the cart for the price to be applied.
38
39 This is useful to set tiered pricing for resources like product variants and shipping options.
40
41 For example, you can set a variant's price to:
42
43 - `$10` by default.
44 - `$8` when the customer adds `10` or more of the variant to the cart.
45 - `$6` when the customer adds `20` or more of the variant to the cart.
46
47 These price definitions would look like this:
48
49 ```json title="Example Prices"
50 [
51 // default price
52 {
53 "amount": 10,
54 "currency_code": "usd",
55 },
56 {
57 "amount": 8,
58 "currency_code": "usd",
59 "min_quantity": 10,
60 "max_quantity": 19,
61 },
62 {
63 "amount": 6,
64 "currency_code": "usd",
65 "min_quantity": 20,
66 },
67 ],
68 ```
69
70 ### How to Create Tiered Prices?
71
72 When you create prices, you can specify a `min_quantity` and `max_quantity` for each price. This allows you to create tiered pricing, where the price changes based on the quantity of items in the cart.
73
74 For example:
75
76 <Note>
77
78 For most use cases where you're building customizations in the Medusa application, it's highly recommended to use [Medusa's workflows](../../../medusa-workflows-reference/page.mdx) rather than using the Pricing Module directly. Medusa's workflows already implement extensive functionalities that you can re-use in your custom flows, with reliable roll-back mechanism.
79
80 </Note>
81
82 <CodeTabs group="price-example">
83 <CodeTab label="Using Medusa Workflows" value="workflows">
84
85 export const tieredPricingHighlights = [
86 ["20", "min_quantity", "The minimum quantity that must be in the cart for the price to be applied."],
87 ["21", "max_quantity", "The maximum quantity that can be in the cart for the price to be applied."],
88 ]
89
90 ```ts highlights={tieredPricingHighlights}
91 import { createProductsWorkflow } from "@medusajs/medusa/core-flows"
92
93 // ...
94
95 const { result } = await createProductsWorkflow(container)
96 .run({
97 input: {
98 products: [{
99 variants: [{
100 id: "variant_1",
101 prices: [
102 // default price
103 {
104 amount: 10,
105 currency_code: "usd",
106 },
107 {
108 amount: 8,
109 currency_code: "usd",
110 min_quantity: 10,
111 max_quantity: 19,
112 },
113 {
114 amount: 6,
115 currency_code: "usd",
116 min_quantity: 20,
117 },
118 ],
119 // ...
120 }],
121 }],
122 // ...
123 },
124 })
125 ```
126
127 </CodeTab>
128 <CodeTab label="Using the Pricing Module" value="pricing-module">
129
130 ```ts
131 const priceSet = await pricingModule.addPrices({
132 priceSetId: "pset_1",
133 prices: [
134 // default price
135 {
136 amount: 10,
137 currency_code: "usd",
138 },
139 // tiered prices
140 {
141 amount: 8,
142 currency_code: "usd",
143 min_quantity: 10,
144 max_quantity: 19,
145 },
146 {
147 amount: 6,
148 currency_code: "usd",
149 min_quantity: 20,
150 },
151 ],
152 })
153 ```
154
155 </CodeTab>
156 </CodeTabs>
157
158 In this example, you create a product with a variant whose default price is `$10`. You also add two tiered prices that set the price to `$8` when the quantity is between `10` and `19`, and to `$6` when the quantity is `20` or more.
159
160 ### How are Tiered Prices Applied?
161
162 The [price calculation](../price-calculation/page.mdx) mechanism considers the cart's items as a context when choosing the best price to apply.
163
164 For example, consider the customer added the `variant_1` product variant (created in the workflow snippet of the [above section](#how-to-create-tiered-prices)) to their cart with a quantity of `15`.
165
166 The price calculation mechanism will choose the second price, which is `$8`, because the quantity of `15` is between `10` and `19`.
167
168 <Note>
169
170 If there are other rules applied to the price, they may affect the price calculation. Keep reading to learn about other price rules, and refer to the [Price Calculation](../price-calculation/page.mdx) guide for more details on the calculation mechanism.
171
172 </Note>
173
174 ---
175
176 ## Price Rule
177
178 You can also restrict prices by advanced rules, such as a customer's group, zip code, or a cart's total.
179
180 Each rule of a price is represented by the [PriceRule data model](/references/pricing/models/PriceRule).
181
182 The `Price` data model has a `rules_count` property, which indicates how many rules, represented by `PriceRule`, are applied to the price.
183
184 For example, you create a price restricted to `10557` zip codes.
185
186 ![A diagram showcasing the relation between the PriceRule and Price](https://res.cloudinary.com/dza7lstvk/image/upload/v1709648772/Medusa%20Resources/price-rule-1_vy8bn9.jpg)
187
188 A price can have multiple price rules.
189
190 For example, a price can be restricted by a region and a zip code.
191
192 ![A diagram showcasing the relation between the PriceRule and Price with multiple rules.](https://res.cloudinary.com/dza7lstvk/image/upload/v1709649296/Medusa%20Resources/price-rule-3_pwpocz.jpg)
193
194 ### Price List Rules
195
196 Rules applied to a price list are represented by the [PriceListRule data model](/references/pricing/models/PriceListRule).
197
198 The `rules_count` property of a `PriceList` indicates how many rules are applied to it.
199
200 ![A diagram showcasing the relation between the PriceSet, PriceList, Price, RuleType, and PriceListRuleValue](https://res.cloudinary.com/dza7lstvk/image/upload/v1709641999/Medusa%20Resources/price-list_zd10yd.jpg)
201
202 ### How to Create Prices with Rules?
203
204 When you create prices, you can specify rules for each price. This allows you to create complex pricing strategies based on different contexts.
205
206 For example:
207
208 <Note>
209
210 For most use cases where you're building customizations in the Medusa application, it's highly recommended to use [Medusa's workflows](../../../medusa-workflows-reference/page.mdx) rather than using the Pricing Module directly. Medusa's workflows already implement extensive functionalities that you can re-use in your custom flows, with reliable roll-back mechanism.
211
212 </Note>
213
214 <CodeTabs group="price-example">
215 <CodeTab label="Using Medusa Workflows" value="workflows">
216
217 export const workflowHighlights = [
218 ["19", "rules", "The default price doesn't have rules."],
219 ["27", "attribute", "Apply the price if the cart or order's total matches the condition."]
220 ]
221
222 ```ts highlights={workflowHighlights}
223 const { result } = await createShippingOptionsWorkflow(container)
224 .run({
225 input: [{
226 name: "Standard Shipping",
227 service_zone_id: "serzo_123",
228 shipping_profile_id: "sp_123",
229 provider_id: "prov_123",
230 type: {
231 label: "Standard",
232 description: "Standard shipping",
233 code: "standard",
234 },
235 price_type: "flat",
236 prices: [
237 // default price
238 {
239 currency_code: "usd",
240 amount: 10,
241 rules: [],
242 },
243 // price if cart total >= $100
244 {
245 currency_code: "usd",
246 amount: 0,
247 rules: [
248 {
249 attribute: "item_total",
250 operator: "gte",
251 value: 100,
252 },
253 ],
254 },
255 ],
256 }],
257 })
258 ```
259
260 </CodeTab>
261 <CodeTab label="Using the Pricing Module" value="pricing-module">
262
263 ```ts
264 const priceSet = await pricingModule.addPrices({
265 priceSetId: "pset_1",
266 prices: [
267 // default price
268 {
269 currency_code: "usd",
270 amount: 10,
271 rules: {},
272 },
273 // price if cart total >= $100
274 {
275 currency_code: "usd",
276 amount: 0,
277 rules: {
278 item_total: {
279 operator: "gte",
280 value: 100,
281 },
282 },
283 },
284 ],
285 })
286 ```
287
288 </CodeTab>
289 </CodeTabs>
290
291 In this example, you create a shipping option whose default price is `$10`. When the total of the cart or order using this shipping option is greater than `$100`, the shipping option's price becomes free.
292
293 <Note title="Price rules apply to flat-rate options only">
294
295 Price rules only affect shipping options whose `price_type` is `flat`. For options with a `price_type` of `calculated`, the fulfillment provider determines the price during checkout, so Medusa ignores any price rules you set. To offer free shipping above a threshold, use a flat-rate shipping option.
296
297 </Note>
298
299 ### How is the Price Rule Applied?
300
301 The [price calculation](../price-calculation/page.mdx) mechanism considers a price applicable when the resource that this price is in matches the specified rules.
302
303 For example, a [cart object](!api!/store/carts/schema) has an `item_total` property. So, if a shipping option has the following price:
304
305 ```json
306 {
307 "currency_code": "usd",
308 "amount": 0,
309 "rules": {
310 "item_total": {
311 "operator": "gte",
312 "value": 100,
313 }
314 }
315 }
316 ```
317
318 The shipping option's price is applied when the cart's `item_total` is greater than or equal to `$100`.
319
320 <Note title="What item_total represents">
321
322 The cart's `item_total` is the sum of its line-item totals after discounts and including taxes. It doesn't include shipping, so it can differ from the total shown to the customer at checkout. Set the rule's `value` based on the items' total, not the cart's grand total.
323
324 </Note>
325
326 You can also apply the rule on nested relations and properties. For example, to apply a shipping option's price based on the customer's group, you can apply a rule on the `customer.group.id` attribute:
327
328 ```json
329 {
330 "currency_code": "usd",
331 "amount": 0,
332 "rules": {
333 "customer.group.id": {
334 "operator": "eq",
335 "value": "cusgrp_123"
336 }
337 }
338 }
339 ```
340
341 In this example, the price is only applied if a cart's customer belongs to the customer group of ID `cusgrp_123`.
342
343 <Note>
344
345 These same rules apply to product variant prices as well, or any other resource that has a price.
346
347 </Note>
348
349 ---
350
351 ## Examples
352
353 This section provides some examples of implementing price tiers and rules for products and shipping options.
354
355 ### Product Variant Price by Quantity
356
357 <CodeTabs group="product-tier-example">
358 <CodeTab label="Using Medusa Workflows" value="workflows">
359
360 ```ts
361 import { createProductsWorkflow } from "@medusajs/medusa/core-flows"
362
363 // ...
364
365 const { result } = await createProductsWorkflow(container)
366 .run({
367 input: {
368 products: [{
369 title: "Premium T-Shirt",
370 variants: [{
371 title: "Small / Black",
372 prices: [
373 // default price
374 {
375 amount: 20,
376 currency_code: "usd",
377 },
378 // buy 5 or more, get a small discount
379 {
380 amount: 18,
381 currency_code: "usd",
382 min_quantity: 5,
383 max_quantity: 9,
384 },
385 // buy 10 or more, get a bigger discount
386 {
387 amount: 15,
388 currency_code: "usd",
389 min_quantity: 10,
390 },
391 ],
392 }],
393 }],
394 },
395 })
396 ```
397
398 </CodeTab>
399 <CodeTab label="Using the Pricing Module" value="pricing-module">
400
401 ```ts
402 const priceSet = await pricingModule.addPrices({
403 // this is the price set of the product variant
404 priceSetId: "pset_product_123",
405 prices: [
406 // default price: $20.00
407 {
408 amount: 20,
409 currency_code: "usd",
410 },
411 // buy 5-9 shirts: $18.00 each
412 {
413 amount: 18,
414 currency_code: "usd",
415 min_quantity: 5,
416 max_quantity: 9,
417 },
418 // buy 10+ shirts: $15.00 each
419 {
420 amount: 15,
421 currency_code: "usd",
422 min_quantity: 10,
423 },
424 ],
425 })
426 ```
427
428 </CodeTab>
429 </CodeTabs>
430
431 ### Product Variant Price by Customer Group
432
433 <CodeTabs group="product-rule-example">
434 <CodeTab label="Using Medusa Workflows" value="workflows">
435
436 ```ts
437 import { createProductsWorkflow } from "@medusajs/medusa/core-flows"
438
439 // ...
440
441 const { result } = await createProductsWorkflow(container)
442 .run({
443 input: {
444 products: [{
445 title: "Premium T-Shirt",
446 variants: [{
447 title: "Small / Black",
448 prices: [
449 // default price
450 {
451 amount: 40,
452 currency_code: "usd",
453 },
454 // discounted price for VIP customers
455 {
456 amount: 30,
457 currency_code: "usd",
458 rules: {
459 "customer.groups.id": "cusgrp_vip123",
460 },
461 },
462 // special price for wholesale customers
463 {
464 amount: 20,
465 currency_code: "usd",
466 rules: {
467 "customer.groups.id": "cusgrp_wholesale456",
468 },
469 },
470 ],
471 }],
472 }],
473 },
474 })
475 ```
476
477 </CodeTab>
478 <CodeTab label="Using the Pricing Module" value="pricing-module">
479
480 ```ts
481 const priceSet = await pricingModule.addPrices({
482 // this is the price set of the product variant
483 priceSetId: "pset_product_123",
484 prices: [
485 // default price
486 {
487 amount: 40,
488 currency_code: "usd",
489 },
490 // discounted price for VIP customers
491 {
492 amount: 30,
493 currency_code: "usd",
494 rules: {
495 "customer.groups.id": "cusgrp_vip123",
496 },
497 },
498 // special price for wholesale customers
499 {
500 amount: 20,
501 currency_code: "usd",
502 rules: {
503 "customer.groups.id": "cusgrp_wholesale456",
504 },
505 },
506 ],
507 })
508 ```
509
510 </CodeTab>
511 </CodeTabs>
512
513 ### Free Shipping for Orders Above a Certain Amount
514
515 <CodeTabs group="shipping-tier-example">
516 <CodeTab label="Using Medusa Workflows" value="workflows">
517
518 ```ts
519 const { result } = await createShippingOptionsWorkflow(container)
520 .run({
521 input: [{
522 name: "Standard Shipping",
523 service_zone_id: "serzo_123",
524 shipping_profile_id: "sp_123",
525 provider_id: "prov_123",
526 type: {
527 label: "Standard",
528 description: "Standard shipping (2-5 business days)",
529 code: "standard",
530 },
531 price_type: "flat",
532 prices: [
533 // regular shipping price
534 {
535 currency_code: "usd",
536 amount: 10,
537 rules: [],
538 },
539 // free shipping for carts over $100
540 {
541 currency_code: "usd",
542 amount: 0,
543 rules: [
544 {
545 attribute: "item_total",
546 operator: "gte",
547 value: 100,
548 },
549 ],
550 },
551 ],
552 }],
553 })
554 ```
555
556 </CodeTab>
557 <CodeTab label="Using the Pricing Module" value="pricing-module">
558
559 ```ts
560 const priceSet = await pricingModule.addPrices({
561 priceSetId: "pset_shipping_123",
562 prices: [
563 // regular shipping price
564 {
565 currency_code: "usd",
566 amount: 10,
567 rules: {},
568 },
569 // free shipping for orders over $100
570 {
571 currency_code: "usd",
572 amount: 0,
573 rules: {
574 item_total: [
575 {
576 operator: "gte",
577 value: 100,
578 },
579 ],
580 },
581 },
582 ],
583 })
584 ```
585
586 </CodeTab>
587 </CodeTabs>
588
589 ### Shipping Prices Based on Customer Groups
590
591 <CodeTabs group="shipping-customer-example">
592 <CodeTab label="Using Medusa Workflows" value="workflows">
593
594 ```ts
595 const { result } = await createShippingOptionsWorkflow(container)
596 .run({
597 input: [{
598 name: "Express Shipping",
599 service_zone_id: "serzo_456",
600 shipping_profile_id: "sp_456",
601 provider_id: "prov_456",
602 type: {
603 label: "Express",
604 description: "Express shipping (1-2 business days)",
605 code: "express",
606 },
607 price_type: "flat",
608 prices: [
609 // regular express shipping price
610 {
611 currency_code: "usd",
612 amount: 20,
613 rules: [],
614 },
615 // discounted express shipping for VIP customers
616 {
617 currency_code: "usd",
618 amount: 15,
619 rules: [
620 {
621 attribute: "customer.groups.id",
622 operator: "in",
623 value: ["cusgrp_vip123"],
624 },
625 ],
626 },
627 // special rate for B2B customers
628 {
629 currency_code: "usd",
630 amount: 13,
631 rules: [
632 {
633 attribute: "customer.groups.id",
634 operator: "in",
635 value: ["cusgrp_b2b789"],
636 },
637 ],
638 },
639 ],
640 }],
641 })
642 ```
643
644 </CodeTab>
645 <CodeTab label="Using the Pricing Module" value="pricing-module">
646
647 ```ts
648 const priceSet = await pricingModule.addPrices({
649 priceSetId: "pset_shipping_456",
650 prices: [
651 // regular express shipping
652 {
653 currency_code: "usd",
654 amount: 20,
655 rules: {},
656 },
657 // VIP customer express shipping
658 {
659 currency_code: "usd",
660 amount: 15,
661 rules: {
662 "customer.groups.id": [
663 {
664 operator: "in",
665 value: ["cusgrp_vip123"],
666 },
667 ],
668 },
669 },
670 // B2B customer express shipping
671 {
672 currency_code: "usd",
673 amount: 13,
674 rules: {
675 "customer.groups.id": [
676 {
677 operator: "in",
678 value: ["cusgrp_b2b789"],
679 },
680 ],
681 },
682 },
683 ],
684 })
685 ```
686
687 </CodeTab>
688 </CodeTabs>
689
690 ### Combined Tiered and Rule-Based Shipping Pricing
691
692 <CodeTabs group="shipping-combined-example">
693 <CodeTab label="Using Medusa Workflows" value="workflows">
694
695 ```ts
696 const { result } = await createShippingOptionsWorkflow(container)
697 .run({
698 input: [{
699 name: "Bulk Shipping",
700 service_zone_id: "serzo_789",
701 shipping_profile_id: "sp_789",
702 provider_id: "prov_789",
703 type: {
704 label: "Bulk",
705 description: "Shipping for bulk orders",
706 code: "bulk",
707 },
708 price_type: "flat",
709 prices: [
710 // base shipping price
711 {
712 currency_code: "usd",
713 amount: 20,
714 rules: [],
715 },
716 // discounted shipping for 5-10 items
717 {
718 currency_code: "usd",
719 amount: 18,
720 min_quantity: 5,
721 max_quantity: 10,
722 rules: [],
723 },
724 // heavily discounted shipping for 10+ items
725 {
726 currency_code: "usd",
727 amount: 15,
728 min_quantity: 11,
729 rules: [],
730 },
731 // free shipping for VIP customers with carts over $200
732 {
733 currency_code: "usd",
734 amount: 0,
735 rules: {
736 "customer.groups.id": [
737 {
738 operator: "in",
739 value: ["cusgrp_vip123"],
740 },
741 ],
742 item_total: [
743 {
744 operator: "gte",
745 value: 200,
746 },
747 ],
748 },
749 },
750 ],
751 }],
752 })
753 ```
754
755 </CodeTab>
756 <CodeTab label="Using the Pricing Module" value="pricing-module">
757
758 ```ts
759 const priceSet = await pricingModule.addPrices({
760 priceSetId: "pset_shipping_789",
761 prices: [
762 // base shipping price: $20.00
763 {
764 currency_code: "usd",
765 amount: 20,
766 rules: {},
767 },
768 // 5-10 items: $18.00
769 {
770 currency_code: "usd",
771 amount: 18,
772 min_quantity: 5,
773 max_quantity: 10,
774 rules: {},
775 },
776 // 11+ items: $15.00
777 {
778 currency_code: "usd",
779 amount: 15,
780 min_quantity: 11,
781 rules: {},
782 },
783 // VIP customers with orders over $200: FREE
784 {
785 currency_code: "usd",
786 amount: 0,
787 rules: [
788 {
789 attribute: "customer.groups.id",
790 operator: "in",
791 value: ["cusgrp_vip123"],
792 },
793 {
794 attribute: "item_total",
795 operator: "gte",
796 value: 200,
797 },
798 ],
799 },
800 ],
801 })
802 ```
803
804 </CodeTab>
805 </CodeTabs>
806
807 ### Shipping Prices Based on Geographical Rules
808
809 <CodeTabs group="shipping-geo-example">
810 <CodeTab label="Using Medusa Workflows" value="workflows">
811
812 ```ts
813 const { result } = await createShippingOptionsWorkflow(container)
814 .run({
815 input: [{
816 name: "Regional Shipping",
817 service_zone_id: "serzo_geo123",
818 shipping_profile_id: "sp_standard",
819 provider_id: "prov_standard",
820 type: {
821 label: "Standard",
822 description: "Standard shipping with regional pricing",
823 code: "standard_regional",
824 },
825 price_type: "flat",
826 prices: [
827 // default shipping price
828 {
829 currency_code: "usd",
830 amount: 15,
831 rules: [],
832 },
833 // special price for specific zip codes (metropolitan areas)
834 {
835 currency_code: "usd",
836 amount: 10,
837 rules: [
838 {
839 attribute: "shipping_address.postal_code",
840 operator: "in",
841 value: ["10001", "10002", "10003", "90001", "90002", "90003"],
842 },
843 ],
844 },
845 // higher price for remote areas
846 {
847 currency_code: "usd",
848 amount: 25,
849 rules: [
850 {
851 attribute: "shipping_address.postal_code",
852 operator: "in",
853 value: ["99501", "99502", "99503", "00601", "00602", "00603"],
854 },
855 ],
856 },
857 // different price for a specific region
858 {
859 currency_code: "usd",
860 amount: 12,
861 rules: [
862 {
863 attribute: "region.id",
864 operator: "eq",
865 value: "reg_123",
866 },
867 ],
868 },
869 // different price for a specific country
870 {
871 currency_code: "usd",
872 amount: 20,
873 rules: [
874 {
875 attribute: "shipping_address.country_code",
876 operator: "eq",
877 value: "ca",
878 },
879 ],
880 },
881 ],
882 }],
883 })
884 ```
885
886 </CodeTab>
887 <CodeTab label="Using the Pricing Module" value="pricing-module">
888
889 ```ts
890 const priceSet = await pricingModule.addPrices({
891 priceSetId: "pset_shipping_geo",
892 prices: [
893 // default shipping price: $15.00
894 {
895 currency_code: "usd",
896 amount: 15,
897 rules: {},
898 },
899 // metropolitan areas: $10.00
900 {
901 currency_code: "usd",
902 amount: 10,
903 rules: {
904 "shipping_address.postal_code": [
905 {
906 operator: "in",
907 value: ["10001", "10002", "10003", "90001", "90002", "90003"],
908 },
909 ],
910 },
911 },
912 // remote areas: $25.00
913 {
914 currency_code: "usd",
915 amount: 25,
916 rules: {
917 "shipping_address.postal_code": [
918 {
919 operator: "in",
920 value: ["99501", "99502", "99503", "00601", "00602", "00603"],
921 },
922 ],
923 },
924 },
925 // Northeast region: $12.00
926 {
927 currency_code: "usd",
928 amount: 12,
929 rules: {
930 "region.id": "reg_123",
931 },
932 },
933 // Canada: $20.00
934 {
935 currency_code: "usd",
936 amount: 20,
937 rules: {
938 "shipping_address.country_code": "ca",
939 },
940 },
941 ],
942 })
943 ```
944
945 </CodeTab>
946 </CodeTabs>