Introduction

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:

  • Create, update, and delete discount rules.
  • Retrieve discount rules.

Before you begin

  • Currently only item-level discounts are supported. Discounts for an entire cart/checkout are not yet supported.
  • Up to 5 triggers can be chained together.

To use the Discount Rules API, import { discountRules } from the wix-ecom-backend module:

Copy
import { discountRules } from "wix-ecom-backend";

Terminology

  • 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.

    • Discounts must have a defined scope/s.
  • Scope: A group of catalog items that qualify for a discount.

    • Every catalog has 2 scopes "out of the box". For example, Wix Stores has Specific Products and All Products scopes.
    • Scopes are required in default triggers and discount objects.
    • Triggers and discounts can have multiple 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.

    • Default triggers: These built-in triggers fire when a specified minimum/maximum item quantity (for example, "at least 5 items") or cart subtotal ("no more than $100") is reached. For this trigger to fire, the items must also be part of a defined scope.
Did this help?

createDiscountRule( )


Creates a new discount rule.

The createDiscountRule() function returns a Promise that resolves to the new discount rule when it's created.

Authentication

This function requires elevated permissions and runs only on the backend and on dashboard pages.

Permissions
Manage Stores - all permissions
Manage eCommerce - all permissions
Learn more about app permissions.
Method Declaration
Copy
function createDiscountRule(discountRule: DiscountRule): Promise<DiscountRule>;
Method Parameters
discountRuleDiscountRuleRequired

Discount rule info.

Returns
Return Type:Promise<DiscountRule>
JavaScript
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 } }
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?

deleteDiscountRule( )


Deletes a discount rule.

The deleteDiscountRule() function returns a Promise that resolves when the specified discount rule is deleted.

Authentication

This function requires elevated permissions and runs only on the backend and on dashboard pages.

Permissions
Manage Stores - all permissions
Manage eCommerce - all permissions
Learn more about app permissions.
Method Declaration
Copy
function deleteDiscountRule(discountRuleId: string): Promise<void>;
Method Parameters
discountRuleIdstringRequired

ID of the discount rule to delete.

JavaScript
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 } }
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?

getDiscountRule( )


Retrieves a discount rule.

The getDiscountRule() function returns a Promise that resolves when the specified discount rule is retrieved.

Authentication

This function requires elevated permissions and runs only on the backend and on dashboard pages.

Permissions
Manage Stores - all permissions
Read eCommerce - all read permissions
Read Stores - all read permissions
Manage eCommerce - all permissions
Learn more about app permissions.
Method Declaration
Copy
function getDiscountRule(discountRuleId: string): Promise<DiscountRule>;
Method Parameters
discountRuleIdstringRequired

ID of the discount rule to retrieve.

Returns
Return Type:Promise<DiscountRule>
JavaScript
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 } }
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?

queryDiscountRules( )


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.

PROPERTYSUPPORTED FILTERS & SORTING
_ideq(),ne(),exists(),in(),hasSome(),startsWith(),ascending(),descending()
revisioneq(),ne(),exists(),in(),hasSome(),lt(),le(),gt(),ge(),ascending(),descending()
_createdDateeq(),ne(),exists(),in(),hasSome(),lt(),le(),gt(),ge(),ascending(),descending()
_updatedDateeq(),ne(),exists(),in(),hasSome(),lt(),le(),gt(),ge(),ascending(),descending()
activeeq(),ne(),exists(),in(),hasSome(),ascending(),descending()
nameeq(),ne(),exists(),in(),hasSome(),startsWith(),ascending(),descending()
activeTimeInfo.starteq(),ne(),exists(),in(),hasSome(),lt(),le(),gt(),ge(),ascending(),descending()
activeTimeInfo.endeq(),ne(),exists(),in(),hasSome(),lt(),le(),gt(),ge(),ascending(),descending()
Authentication

This function requires elevated permissions and runs only on the backend and on dashboard pages.

Permissions
Manage Stores - all permissions
Read eCommerce - all read permissions
Read Stores - all read permissions
Manage eCommerce - all permissions
Learn more about app permissions.
Method Declaration
Copy
function queryDiscountRules(): DiscountRulesQueryBuilder;
Request
This method does not take any parameters
Returns
JavaScript
import { discountRules } from "wix-ecom-backend"; async function queryDiscountRules() { const { items } = discountRules.queryDiscountRules().find(); }
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?

updateDiscountRule( )


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.

Authentication

This function requires elevated permissions and runs only on the backend and on dashboard pages.

Permissions
Manage Stores - all permissions
Manage eCommerce - all permissions
Learn more about app permissions.
Method Declaration
Copy
function updateDiscountRule(
  _id: string,
  discountRule: UpdateDiscountRule,
  options: UpdateDiscountRuleOptions,
): Promise<DiscountRule>;
Method Parameters
_idstringRequired

Discount rule ID.


discountRuleUpdateDiscountRuleRequired

Discount rule info.


optionsUpdateDiscountRuleOptions

Discount rule info.

Returns
Return Type:Promise<DiscountRule>
JavaScript
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 } }
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?