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
idstringRequiredregionIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the discount's details.
ResponsePromiseResponsePromise<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.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the discount's details.
ResponsePromiseResponsePromise<AdminDiscountsRes>Requiredupdate
Update a discount with a given set of rules that define how the discount is applied.
Example
Parameters
idstringRequiredThe attributes to update in the discount.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<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
idstringRequiredThe dynamic code to create.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<AdminDiscountsRes>Requireddelete
Delete a discount. Deleting the discount will make it unavailable for customers to use.
Example
Parameters
idstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<DeleteResponse>RequiredResolves to the delete operation details.
ResponsePromiseResponsePromise<DeleteResponse>RequireddeleteDynamicCode
Delete a dynamic code from a discount.
Example
Parameters
idstringRequiredcodestringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<AdminDiscountsRes>Requiredretrieve
Retrieve a discount.
Example
Parameters
idstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Configurations to apply on the retrieved product category.
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredretrieveByCode
Retrieve a discount's details by its discount code.
Example
Parameters
codestringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<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
customHeadersRecord<string, any>RequiredDefault: {}
Filters and pagination configurations to apply on the retrieved discounts.
Returns
ResponsePromiseResponsePromise<AdminDiscountsListRes>RequiredResolves to the list of discounts with pagination fields.
ResponsePromiseResponsePromise<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
idstringRequiredregionIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<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
discountIdstringRequiredThe discount condition to create.
Configurations to apply on the returned discount.Default: {}
{}customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<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
discountIdstringRequiredconditionIdstringRequiredThe attributes to update in the discount condition.
Configurations to apply on the returned discount.Default: {}
{}customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<AdminDiscountsRes>RequireddeleteCondition
Delete a discount condition. This doesn't delete resources associated to the discount condition.
Example
Parameters
discountIdstringRequiredconditionIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<DeleteResponse>RequiredResolves to the deletion operation details.
ResponsePromiseResponsePromise<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
discountIdstringRequiredconditionIdstringRequiredcustomHeadersRecord<string, any>RequiredDefault: {}
Configurations to apply on the retrieved discount condition.
Returns
ResponsePromiseResponsePromise<AdminDiscountConditionsRes>RequiredResolves to the discount condition details.
ResponsePromiseResponsePromise<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
discountIdstringRequiredconditionIdstringRequiredThe resources to add to the discount condition.
customHeadersRecord<string, any>RequiredDefault: {}
Configurations to apply on the retrieved discount.
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<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
discountIdstringRequiredconditionIdstringRequiredThe resources to remove.
customHeadersRecord<string, any>RequiredDefault: {}
Returns
ResponsePromiseResponsePromise<AdminDiscountsRes>RequiredResolves to the details of the discount.
ResponsePromiseResponsePromise<AdminDiscountsRes>Required