The Discount Rules API allows you to create and manage discount rules. Discount rules are sets of triggers and scopes that together define the necessary conditions for a discount to apply to items in the cart/checkout. Discount rules are known as automatic discounts in the dashboard.
With the Discount Rules API, you can:
To use the Discount Rules API, import { discountRules }
from the wix-ecom-backend
module:
import { discountRules } from "wix-ecom-backend";
Discount rule: A set of conditions (scope and trigger) that dictate whether an item/product qualifies for a specified discount.
Discount: The change applied to an item's price when conditions are met. Discounts can reduce an item's price by percentage or a specified amount, and also by setting an item to a fixed price.
Scope: A group of catalog items that qualify for a discount.
Specific Products
and All Products
scopes.Trigger: A set of conditions that must be met for a discount to become applicable. Triggers can be chained so that more than 1 condition must be met.
Creates a new discount rule.
The createDiscountRule()
function returns a Promise that resolves to the new discount rule when it's created.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function createDiscountRule(discountRule: DiscountRule): Promise<DiscountRule>;
Discount rule info.
import { discountRules } from "wix-ecom-backend";
async function createDiscountRule(discountRule) {
try {
const result = await discountRules.createDiscountRule(discountRule);
return result;
} catch (error) {
console.error(error);
// Handle the error
}
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Deletes a discount rule.
The deleteDiscountRule()
function returns a Promise that resolves when the specified discount rule is deleted.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function deleteDiscountRule(discountRuleId: string): Promise<void>;
ID of the discount rule to delete.
import { discountRules } from "wix-ecom-backend";
async function deleteDiscountRule(discountRuleId) {
try {
const result = await discountRules.deleteDiscountRule(discountRuleId);
return result;
} catch (error) {
console.error(error);
// Handle the error
}
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Retrieves a discount rule.
The getDiscountRule()
function returns a Promise that resolves when the specified discount rule is retrieved.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function getDiscountRule(discountRuleId: string): Promise<DiscountRule>;
ID of the discount rule to retrieve.
import { discountRules } from "wix-ecom-backend";
async function getDiscountRule(discountRuleId) {
try {
const result = await discountRules.getDiscountRule(discountRuleId);
return result;
} catch (error) {
console.error(error);
// Handle the error
}
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Creates a query to retrieve a list of discount rules.
The queryDiscountRules()
function builds a query to retrieve a list of up to 100 discount rules, and returns a DiscountRulesQueryBuilder
object.
The returned object contains the query definition which is typically used to run the query using the find()
function.
You can refine the query by chaining DiscountRulesQueryBuilder
functions onto the query. DiscountRulesQueryBuilder
functions enable you to sort, filter, and control the results queryDiscountRules() returns.
By default, queryDiscountRules()
sorts results by ascending("_id")
by default. This can be overridden.
To learn how to query posts, refer to the table below.
The following DiscountRulesQueryBuilder
functions are supported for the queryDiscountRules()
function. For a full description of the discount rule object, see the object returned for the items
property in the DiscountRulesQueryResult
.
PROPERTY | SUPPORTED FILTERS & SORTING |
---|---|
_id | eq() ,ne() ,exists() ,in() ,hasSome() ,startsWith() ,ascending() ,descending() |
revision | eq() ,ne() ,exists() ,in() ,hasSome() ,lt() ,le() ,gt() ,ge() ,ascending() ,descending() |
_createdDate | eq() ,ne() ,exists() ,in() ,hasSome() ,lt() ,le() ,gt() ,ge() ,ascending() ,descending() |
_updatedDate | eq() ,ne() ,exists() ,in() ,hasSome() ,lt() ,le() ,gt() ,ge() ,ascending() ,descending() |
active | eq() ,ne() ,exists() ,in() ,hasSome() ,ascending() ,descending() |
name | eq() ,ne() ,exists() ,in() ,hasSome() ,startsWith() ,ascending() ,descending() |
activeTimeInfo.start | eq() ,ne() ,exists() ,in() ,hasSome() ,lt() ,le() ,gt() ,ge() ,ascending() ,descending() |
activeTimeInfo.end | eq() ,ne() ,exists() ,in() ,hasSome() ,lt() ,le() ,gt() ,ge() ,ascending() ,descending() |
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function queryDiscountRules(): DiscountRulesQueryBuilder;
import { discountRules } from "wix-ecom-backend";
async function queryDiscountRules() {
const { items } = discountRules.queryDiscountRules().find();
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Updates a discount rule's properties.
The updateDiscountRule()
function returns a Promise that resolves when the specified discount rule's properties are updated.
Each time the discount rule is updated, revision
increments by 1. The existing revision
must be included when updating the discount rule. This ensures you're working with the latest discount rule information, and it prevents unintended overwrites.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function updateDiscountRule(
_id: string,
discountRule: UpdateDiscountRule,
options: UpdateDiscountRuleOptions,
): Promise<DiscountRule>;
Discount rule ID.
Discount rule info.
Discount rule info.
import { discountRules } from "wix-ecom-backend";
async function updateDiscountRule(id, discountRule, options) {
try {
const result = await discountRules.updateDiscountRule(
id,
discountRule,
options,
);
return result;
} catch (error) {
console.error(error);
// Handle the error
}
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.