Skip to main content
Skip to main content

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 user 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

addConditionResourceBatch

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

discountIdstringRequired
The ID of the discount the condition belongs to.
conditionIdstringRequired
The ID of the discount condition.
The resources to add to the discount condition.
Configurations to apply on the retrieved discount.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

addRegion

Add a Region to the list of Regions a Discount can be used in.

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.addRegion(discountId, regionId)
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
regionIdstringRequired
The ID of the region to add.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the discount's details.

create

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

payloadAdminPostDiscountsReqRequired
The discount to create.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the discount's details.

createCondition

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

discountIdstringRequired
The discount's ID.
The discount condition to create.
Configurations to apply on the returned discount.

Default: {}

customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

createDynamicCode

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

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.createDynamicCode(discountId, {
code: "TEST",
usage_limit: 1
})
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
The dynamic code to create.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

delete

Delete a discount. Deleting the discount will make it unavailable for customers to use.

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.delete(discountId)
.then(({ id, object, deleted }) => {
console.log(id);
})

Parameters

idstringRequired
The discount's ID.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<DeleteResponse>Required
Resolves to the delete operation details.

deleteCondition

Delete a discount condition. This doesn't delete resources associated to the discount condition.

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.deleteCondition(discountId, conditionId)
.then(({ id, object, deleted }) => {
console.log(id);
})

Parameters

discountIdstringRequired
The discount's ID.
conditionIdstringRequired
The ID of the discount condition.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<DeleteResponse>Required
Resolves to the deletion operation details.

deleteConditionResourceBatch

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

discountIdstringRequired
The ID of the discount the condition belongs to.
conditionIdstringRequired
The ID of the discount condition.
The resources to remove.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

deleteDynamicCode

Delete a dynamic code from a discount.

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.deleteDynamicCode(discountId, code)
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
codestringRequired
The code of the dynamic code to delete.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

getCondition

Retrieve a Discount Condition's details.

Example

A simple example that retrieves a discount condition by its ID:

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)
.then(({ discount_condition }) => {
console.log(discount_condition.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

discountIdstringRequired
The ID of the discount that the condition belongs to.
conditionIdstringRequired
The ID of the discount condition.
Configurations to apply on the retrieved discount condition.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountConditionsRes>Required
Resolves to the discount condition details.

list

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:

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.list()
.then(({ discounts, limit, offset, count }) => {
console.log(discounts.id);
})

To specify relations that should be retrieved within the discounts:

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.list({
expand: "rule"
})
.then(({ discounts, limit, offset, count }) => {
console.log(discounts.id);
})

By default, only the first 20 records are retrieved. You can control pagination by specifying the limit and offset properties:

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.list({
expand: "rule",
limit,
offset
})
.then(({ discounts, limit, offset, count }) => {
console.log(discounts.id);
})

Parameters

Filters and pagination configurations to apply on the retrieved discounts.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsListRes>Required
Resolves to the list of discounts with pagination fields.

removeRegion

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

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.removeRegion(discountId, regionId)
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
regionIdstringRequired
The ID of the region to remove.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

retrieve

Retrieve a discount.

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.retrieve(discountId)
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

retrieveByCode

Retrieve a discount's details by its discount code.

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.retrieveByCode(code)
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

codestringRequired
The code of the discount.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

update

Update a discount with a given set of rules that define how the discount is applied.

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.update(discountId, {
code: "TEST"
})
.then(({ discount }) => {
console.log(discount.id);
})

Parameters

idstringRequired
The discount's ID.
The attributes to update in the discount.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.

updateCondition

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

discountIdstringRequired
The discount's ID.
conditionIdstringRequired
The ID of the discount condition.
The attributes to update in the discount condition.
Configurations to apply on the returned discount.

Default: {}

customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminDiscountsRes>Required
Resolves to the details of the discount.
Was this section helpful?