Skip to main content
Skip to main content

Discounts

Queries and Mutations listed here are used to send requests to the Admin Discount API Routes.

All hooks listed 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.

Mutations

useAdminAddDiscountConditionResourceBatch

This hook adds 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 React from "react"
import {
useAdminAddDiscountConditionResourceBatch
} from "medusa-react"

type Props = {
discountId: string
conditionId: string
}

const DiscountCondition = ({
discountId,
conditionId
}: Props) => {
const addConditionResources = useAdminAddDiscountConditionResourceBatch(
discountId,
conditionId
)
// ...

const handleAdd = (itemId: string) => {
addConditionResources.mutate({
resources: [
{
id: itemId
}
]
}, {
onSuccess: ({ discount }) => {
console.log(discount.id)
}
})
}

// ...
}

export default DiscountCondition

To specify relations to include in the returned discount:

import React from "react"
import {
useAdminAddDiscountConditionResourceBatch
} from "medusa-react"

type Props = {
discountId: string
conditionId: string
}

const DiscountCondition = ({
discountId,
conditionId
}: Props) => {
const addConditionResources = useAdminAddDiscountConditionResourceBatch(
discountId,
conditionId,
{
expand: "rule"
}
)
// ...

const handleAdd = (itemId: string) => {
addConditionResources.mutate({
resources: [
{
id: itemId
}
]
}, {
onSuccess: ({ discount }) => {
console.log(discount.id)
}
})
}

// ...
}

export default DiscountCondition

Hook Parameters

discountIdstringRequired
The ID of the discount the condition belongs to.
conditionIdstringRequired
The discount condition's ID.
Configurations to apply on the retrieved discount.

Mutation Function Parameters

AdminPostDiscountsDiscountConditionsConditionBatchReqAdminPostDiscountsDiscountConditionsConditionBatchReqRequired
The details of the resources to add.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDeleteDiscountConditionResourceBatch

This hook 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 React from "react"
import {
useAdminDeleteDiscountConditionResourceBatch
} from "medusa-react"

type Props = {
discountId: string
conditionId: string
}

const DiscountCondition = ({
discountId,
conditionId
}: Props) => {
const deleteConditionResource = useAdminDeleteDiscountConditionResourceBatch(
discountId,
conditionId,
)
// ...

const handleDelete = (itemId: string) => {
deleteConditionResource.mutate({
resources: [
{
id: itemId
}
]
}, {
onSuccess: ({ discount }) => {
console.log(discount.id)
}
})
}

// ...
}

export default DiscountCondition

Hook Parameters

discountIdstringRequired
The ID of the discount the condition belongs to.
conditionIdstringRequired
The discount condition's ID.

Mutation Function Parameters

AdminDeleteDiscountsDiscountConditionsConditionBatchReqAdminDeleteDiscountsDiscountConditionsConditionBatchReqRequired
The resources to remove.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminCreateDiscount

This hook creates a discount with a given set of rules that defines how the discount is applied.

Example

import React from "react"
import {
useAdminCreateDiscount,
} from "medusa-react"
import {
AllocationType,
DiscountRuleType,
} from "@medusajs/medusa"

const CreateDiscount = () => {
const createDiscount = useAdminCreateDiscount()
// ...

const handleCreate = (
currencyCode: string,
regionId: string
) => {
// ...
createDiscount.mutate({
code: currencyCode,
rule: {
type: DiscountRuleType.FIXED,
value: 10,
allocation: AllocationType.ITEM,
},
regions: [
regionId,
],
is_dynamic: false,
is_disabled: false,
})
}

// ...
}

export default CreateDiscount

Mutation Function Parameters

AdminPostDiscountsReqAdminPostDiscountsReqRequired
The details of the discount to create.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminUpdateDiscount

This hook updates a discount with a given set of rules that define how the discount is applied.

Example

import React from "react"
import { useAdminUpdateDiscount } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const updateDiscount = useAdminUpdateDiscount(discountId)
// ...

const handleUpdate = (isDisabled: boolean) => {
updateDiscount.mutate({
is_disabled: isDisabled,
})
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Parameters

AdminPostDiscountsDiscountReqAdminPostDiscountsDiscountReqRequired
The details of the discount to update.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDeleteDiscount

This hook deletes a discount. Deleting the discount will make it unavailable for customers to use.

Example

import React from "react"
import { useAdminDeleteDiscount } from "medusa-react"

const Discount = () => {
const deleteDiscount = useAdminDeleteDiscount(discount_id)
// ...

const handleDelete = () => {
deleteDiscount.mutate()
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Returned Data

DeleteResponseDeleteResponseRequired
The response returned for a DELETE request.

useAdminDiscountAddRegion

This hook adds a Region to the list of Regions a Discount can be used in.

Example

import React from "react"
import { useAdminDiscountAddRegion } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const addRegion = useAdminDiscountAddRegion(discountId)
// ...

const handleAdd = (regionId: string) => {
addRegion.mutate(regionId, {
onSuccess: ({ discount }) => {
console.log(discount.regions)
}
})
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Parameters

stringstringRequired
The ID of the region to add.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDiscountRemoveRegion

This hook removes 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 React from "react"
import { useAdminDiscountRemoveRegion } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const deleteRegion = useAdminDiscountRemoveRegion(discountId)
// ...

const handleDelete = (regionId: string) => {
deleteRegion.mutate(regionId, {
onSuccess: ({ discount }) => {
console.log(discount.regions)
}
})
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Parameters

stringstringRequired
The ID of the region to remove.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminCreateDynamicDiscountCode

This hook creates 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 React from "react"
import { useAdminCreateDynamicDiscountCode } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const createDynamicDiscount = useAdminCreateDynamicDiscountCode(discountId)
// ...

const handleCreate = (
code: string,
usageLimit: number
) => {
createDynamicDiscount.mutate({
code,
usage_limit: usageLimit
}, {
onSuccess: ({ discount }) => {
console.log(discount.is_dynamic)
}
})
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Parameters

AdminPostDiscountsDiscountDynamicCodesReqAdminPostDiscountsDiscountDynamicCodesReqRequired
The details of the dynamic discount to create.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDeleteDynamicDiscountCode

This hook deletes a dynamic code from a discount.

Example

import React from "react"
import { useAdminDeleteDynamicDiscountCode } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const deleteDynamicDiscount = useAdminDeleteDynamicDiscountCode(discountId)
// ...

const handleDelete = (code: string) => {
deleteDynamicDiscount.mutate(code, {
onSuccess: ({ discount }) => {
console.log(discount.is_dynamic)
}
})
}

// ...
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.

Mutation Function Parameters

stringstringRequired
The code of the dynamic discount to delete.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDiscountCreateCondition

This hook creates 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

import React from "react"
import { DiscountConditionOperator } from "@medusajs/medusa"
import { useAdminDiscountCreateCondition } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const createCondition = useAdminDiscountCreateCondition(discountId)
// ...

const handleCreateCondition = (
operator: DiscountConditionOperator,
products: string[]
) => {
createCondition.mutate({
operator,
products
}, {
onSuccess: ({ discount }) => {
console.log(discount.id)
}
})
}

// ...
}

export default Discount

Hook Parameters

discountIdstringRequired
The discount's ID.

Mutation Function Parameters

AdminPostDiscountsDiscountConditionsAdminPostDiscountsDiscountConditionsRequired
Fields to create or update a discount condition.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDiscountUpdateCondition

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

import React from "react"
import { useAdminDiscountUpdateCondition } from "medusa-react"

type Props = {
discountId: string
conditionId: string
}

const DiscountCondition = ({
discountId,
conditionId
}: Props) => {
const update = useAdminDiscountUpdateCondition(
discountId,
conditionId
)
// ...

const handleUpdate = (
products: string[]
) => {
update.mutate({
products
}, {
onSuccess: ({ discount }) => {
console.log(discount.id)
}
})
}

// ...
}

export default DiscountCondition

Hook Parameters

discountIdstringRequired
The discount's ID.
conditionIdstringRequired
The discount condition's ID.

Mutation Function Parameters

AdminPostDiscountsDiscountConditionsConditionAdminPostDiscountsDiscountConditionsConditionRequired
Fields to create or update a discount condition.

Mutation Function Returned Data

AdminDiscountsResAdminDiscountsResRequired
The discount's details.

useAdminDiscountRemoveCondition

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

Example

import React from "react"
import { useAdminDiscountRemoveCondition } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const deleteCondition = useAdminDiscountRemoveCondition(
discountId
)
// ...

const handleDelete = (
conditionId: string
) => {
deleteCondition.mutate(conditionId, {
onSuccess: ({ id, object, deleted }) => {
console.log(deleted)
}
})
}

// ...
}

export default Discount

Hook Parameters

discountIdstringRequired
The discount's ID.

Mutation Function Parameters

stringstringRequired
The ID of the condition to delete.

Mutation Function Returned Data

DeleteResponseDeleteResponseRequired
The response returned for a DELETE request.

Queries

useAdminDiscounts

This hook retrieves 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 React from "react"
import { useAdminDiscounts } from "medusa-react"

const Discounts = () => {
const { discounts, isLoading } = useAdminDiscounts()

return (
<div>
{isLoading && <span>Loading...</span>}
{discounts && !discounts.length && (
<span>No customers</span>
)}
{discounts && discounts.length > 0 && (
<ul>
{discounts.map((discount) => (
<li key={discount.id}>{discount.code}</li>
))}
</ul>
)}
</div>
)
}

export default Discounts

To specify relations that should be retrieved within the discounts:

import React from "react"
import { useAdminDiscounts } from "medusa-react"

const Discounts = () => {
const { discounts, isLoading } = useAdminDiscounts({
expand: "rule"
})

return (
<div>
{isLoading && <span>Loading...</span>}
{discounts && !discounts.length && (
<span>No customers</span>
)}
{discounts && discounts.length > 0 && (
<ul>
{discounts.map((discount) => (
<li key={discount.id}>{discount.code}</li>
))}
</ul>
)}
</div>
)
}

export default Discounts

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

import React from "react"
import { useAdminDiscounts } from "medusa-react"

const Discounts = () => {
const {
discounts,
limit,
offset,
isLoading
} = useAdminDiscounts({
expand: "rule",
limit: 10,
offset: 0
})

return (
<div>
{isLoading && <span>Loading...</span>}
{discounts && !discounts.length && (
<span>No customers</span>
)}
{discounts && discounts.length > 0 && (
<ul>
{discounts.map((discount) => (
<li key={discount.id}>{discount.code}</li>
))}
</ul>
)}
</div>
)
}

export default Discounts

Hook Parameters

Filters and pagination configurations to apply on the retrieved discounts.

Query Returned Data

limitnumberRequired
The maximum number of items that can be returned in the list.
offsetnumberRequired
The number of items skipped before the returned items in the list.
countnumberRequired
The total number of items available.
discountsDiscount[]Required
The list of discounts.

useAdminDiscount

This hook retrieves a discount.

Example

import React from "react"
import { useAdminDiscount } from "medusa-react"

type Props = {
discountId: string
}

const Discount = ({ discountId }: Props) => {
const { discount, isLoading } = useAdminDiscount(
discountId
)

return (
<div>
{isLoading && <span>Loading...</span>}
{discount && <span>{discount.code}</span>}
</div>
)
}

export default Discount

Hook Parameters

idstringRequired
The discount's ID.
Configurations to apply on the retrieved discount.

Query Returned Data

discountDiscountRequired
Discount details.

useAdminGetDiscountByCode

This hook adds 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

import React from "react"
import { useAdminGetDiscountByCode } from "medusa-react"

type Props = {
discountCode: string
}

const Discount = ({ discountCode }: Props) => {
const { discount, isLoading } = useAdminGetDiscountByCode(
discountCode
)

return (
<div>
{isLoading && <span>Loading...</span>}
{discount && <span>{discount.code}</span>}
</div>
)
}

export default Discount

Hook Parameters

codestringRequired
The code of the discount.

Query Returned Data

discountDiscountRequired
Discount details.

useAdminGetDiscountCondition

This hook retries a Discount Condition's details.

Example

import React from "react"
import { useAdminGetDiscountCondition } from "medusa-react"

type Props = {
discountId: string
discountConditionId: string
}

const DiscountCondition = ({
discountId,
discountConditionId
}: Props) => {
const {
discount_condition,
isLoading
} = useAdminGetDiscountCondition(
discountId,
discountConditionId
)

return (
<div>
{isLoading && <span>Loading...</span>}
{discount_condition && (
<span>{discount_condition.type}</span>
)}
</div>
)
}

export default DiscountCondition

Hook Parameters

idstringRequired
The ID of the discount the condition belongs to.
conditionIdstringRequired
The discount condition's ID.
Configurations to apply on the retrieved discount condition.

Query Returned Data

discount_conditionDiscountConditionRequired
Discount condition details.
Was this section helpful?