AdminDiscountsResource
This class is used to send requests to Admin Discount API Routes. All its method
are available in the JS Client under the medusa.admin.discounts
property.
All methods in this class require authentication.
Admins can create discounts with conditions and rules, providing them with advanced settings for variety of cases. The methods in this class can be used to manage discounts, their conditions, resources, and more.
Related Guide: How to manage discounts.
Methods
addRegion
Add a Region to the list of Regions a Discount can be used in.
Example
Parameters
id
stringRequiredregionId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the discount's details.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Requiredcreate
Create a discount with a given set of rules that defines how the discount is applied.
Example
import Medusa from "@medusajs/medusa-js"
import { AllocationType, DiscountRuleType } from "@medusajs/medusa"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.create({
code: "TEST",
rule: {
type: DiscountRuleType.FIXED,
value: 10,
allocation: AllocationType.ITEM
},
regions: ["reg_XXXXXXXX"],
is_dynamic: false,
is_disabled: false
})
.then(({ discount }) => {
console.log(discount.id);
})
Parameters
The discount to create.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the discount's details.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Requiredupdate
Update a discount with a given set of rules that define how the discount is applied.
Example
Parameters
id
stringRequiredThe attributes to update in the discount.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredcreateDynamicCode
Create a dynamic unique code that can map to a parent discount. This is useful if you want to automatically generate codes with the same rules and conditions.
Example
Parameters
id
stringRequiredThe dynamic code to create.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Requireddelete
Delete a discount. Deleting the discount will make it unavailable for customers to use.
Example
Parameters
id
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<DeleteResponse>RequiredResolves to the delete operation details.
ResponsePromise
ResponsePromise<DeleteResponse>RequireddeleteDynamicCode
Delete a dynamic code from a discount.
Example
Parameters
id
stringRequiredcode
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Requiredretrieve
Retrieve a discount.
Example
Parameters
id
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Configurations to apply on the retrieved product category.
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredretrieveByCode
Retrieve a discount's details by its discount code.
Example
Parameters
code
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Requiredlist
Retrieve a list of Discounts. The discounts can be filtered by fields such as rule
or is_dynamic
. The discounts can also be paginated.
Example
To list discounts:
To specify relations that should be retrieved within the discounts:
By default, only the first 20
records are retrieved. You can control pagination by specifying the limit
and offset
properties:
Parameters
customHeaders
Record<string, any>RequiredDefault: {}
Filters and pagination configurations to apply on the retrieved discounts.
Returns
ResponsePromise
ResponsePromise<AdminDiscountsListRes>RequiredResolves to the list of discounts with pagination fields.
ResponsePromise
ResponsePromise<AdminDiscountsListRes>RequiredremoveRegion
Remove a Region from the list of Regions that a Discount can be used in. This does not delete a region, only the association between it and the discount.
Example
Parameters
id
stringRequiredregionId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredcreateCondition
Create a discount condition. Only one of products
, product_types
, product_collections
, product_tags
, and customer_groups
should be provided in the payload
parameter,
based on the type of discount condition. For example, if the discount condition's type is products
, the products
field should be provided in the payload
parameter.
Example
To create a condition in a discount:
import Medusa from "@medusajs/medusa-js"
import { DiscountConditionOperator } from "@medusajs/medusa"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.createCondition(discountId, {
operator: DiscountConditionOperator.IN,
products: [productId]
})
.then(({ discount }) => {
console.log(discount.id);
})
To specify relations that should be retrieved as part of the response:
import Medusa from "@medusajs/medusa-js"
import { DiscountConditionOperator } from "@medusajs/medusa"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.createCondition(discountId, {
operator: DiscountConditionOperator.IN,
products: [productId]
}, {
expand: "rule"
})
.then(({ discount }) => {
console.log(discount.id);
})
Parameters
discountId
stringRequiredThe discount condition to create.
Configurations to apply on the returned discount.Default: {}
{}
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredupdateCondition
Update a discount condition. Only one of products
, product_types
, product_collections
, product_tags
, and customer_groups
should be provided in the payload
parameter,
based on the type of discount condition. For example, if the discount condition's type is products
, the products
field should be provided in the payload
parameter.
Example
To update a condition in a discount:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.updateCondition(discountId, conditionId, {
products: [
productId
]
})
.then(({ discount }) => {
console.log(discount.id);
})
To specify relations that should be retrieved as part of the response:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.updateCondition(discountId, conditionId, {
products: [
productId
]
}, {
expand: "rule"
})
.then(({ discount }) => {
console.log(discount.id);
})
Parameters
discountId
stringRequiredconditionId
stringRequiredThe attributes to update in the discount condition.
Configurations to apply on the returned discount.Default: {}
{}
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequireddeleteCondition
Delete a discount condition. This doesn't delete resources associated to the discount condition.
Example
Parameters
discountId
stringRequiredconditionId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<DeleteResponse>RequiredResolves to the deletion operation details.
ResponsePromise
ResponsePromise<DeleteResponse>RequiredgetCondition
Retrieve a Discount Condition's details.
Example
A simple example that retrieves a discount condition by its ID:
To specify relations that should be retrieved:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.getCondition(discountId, conditionId, {
expand: "discount_rule"
})
.then(({ discount_condition }) => {
console.log(discount_condition.id);
})
Parameters
discountId
stringRequiredconditionId
stringRequiredcustomHeaders
Record<string, any>RequiredDefault: {}
Configurations to apply on the retrieved discount condition.
Returns
ResponsePromise
ResponsePromise<AdminDiscountConditionsRes>RequiredResolves to the discount condition details.
ResponsePromise
ResponsePromise<AdminDiscountConditionsRes>RequiredaddConditionResourceBatch
Add a batch of resources to a discount condition. The type of resource depends on the type of discount condition. For example, if the discount condition's type is products
,
the resources being added should be products.
Example
To add resources to a discount condition:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.addConditionResourceBatch(discountId, conditionId, {
resources: [{ id: itemId }]
})
.then(({ discount }) => {
console.log(discount.id);
})
To specify relations to include in the returned discount:
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.addConditionResourceBatch(discountId, conditionId, {
resources: [{ id: itemId }]
}, {
expand: "rule"
})
.then(({ discount }) => {
console.log(discount.id);
})
Parameters
discountId
stringRequiredconditionId
stringRequiredThe resources to add to the discount condition.
customHeaders
Record<string, any>RequiredDefault: {}
Configurations to apply on the retrieved discount.
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequireddeleteConditionResourceBatch
Remove a batch of resources from a discount condition. This will only remove the association between the resource and the discount condition, not the resource itself.
Example
import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.discounts.deleteConditionResourceBatch(discountId, conditionId, {
resources: [{ id: itemId }]
})
.then(({ discount }) => {
console.log(discount.id);
})
Parameters
discountId
stringRequiredconditionId
stringRequiredThe resources to remove.
customHeaders
Record<string, any>RequiredDefault: {}
Returns
ResponsePromise
ResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromise
ResponsePromise<AdminDiscountsRes>Required