Events
Events are the primary way to interact with the Promotion Engine. The app publishes events when data becomes available and subscribes to a number of events that can be published by third parties.
The documentation below describes the API as of version 9.x of the script.
Public properties or functions that are not in the discountNinja.api
namespace should be considered obsolete and will be removed in the next major version release.
Using undocumented functions or objects will result in issues as custom code that relies on those functions or objects may break in the future when Discount Ninja's script is automatically updated.
API ready
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Signals that the script has loaded and interaction with the API is possible. |
Syntax
Promotions loaded
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published after promotions have been loaded, processed and filtered by the app. It allows the consumer to inspect which promotions are available for the user during the browsing session. |
Event properties | The event data contains a promotions object. |
Syntax
Product discount calculated
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | This event is published when the (discounted) price of any marked product on the page has been calculated. |
The app will attempt to find all products on a page based on markers in the HTML. It then applies the available promotions to those products and publishes one event per product variant with the results. | |
For the current product, this event is published when the page is loaded and is published again when the selected variant or selected selling plan changes. For other products, this event is published only once per product and per page load. | |
Event properties | The event data contains a product variant price info object. |
Syntax
Cart updated
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the (discounted) cart has been calculated. |
This event is published when the page is loaded and is published again when the content of the cart changes or the set of applied promotions changes. | |
Event properties | The event data contains a discounted cart object. |
Syntax
Cart mutations processing
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the store is making updates to the cart. |
This event is published when Discount Ninja detects that network requests have been made to clear, update, change or add to the cart. Specifically, the app detects fetch requests to the /cart/add.js, /cart/update.js, /cart/change.js and /cart/clear.js (see https://shopify.dev/docs/api/ajax/reference/cart). |
Syntax
Cart mutations processed
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the store has finished making updates to the cart. |
This event is published when Discount Ninja detects that all network requests to the /cart/add.js, /cart/update.js, /cart/change.js and /cart/clear.js (see https://shopify.dev/docs/api/ajax/reference/cart) have been completed. |
Syntax
Trigger checkout
Details
Event |
|
Direction | This event is subscribed to by the app. To publish it, use the following syntax: |
Behavior | This approach is useful if you need to trigger checkout programmatically without bypassing Discount Ninja. Note: if you need a custom checkout pipeline (for example, you may want to execute custom validation logic before allowing the checkout to proceed) when clicking the checkout button, use this API function instead. |
Syntax
Redirect to checkout
Discount Ninja expects users to transfer from the cart to the checkout using a checkout button or link. See this article for more information: https://support.discountninja.io/en/articles/3505661-discount-ninja-checkout-button
As a result, any code that redirects the user to the checkout using window.location
is not supported. The following example shows what such an implementation would look like:
Redirecting a user to the checkout by setting the window.location
causes problems with Discount Ninja. Additionally, it causes other integration issues for the merchant since redirecting without submitting the form has numerous flaws:
The order note field is not submitted if it was changed during this session (and not updated using cart.js)
The locale may not be passed
The discount code present in the cart form is not passed
...
To resolve this, either use a standard checkout button (a button of type="submit" with name="checkout") or change your checkout logic as follows:
Convert money fields
Details
Event |
|
Direction | This event is subscribed to by the app. To publish it, use the following syntax: |
Behavior | Most currency conversion mechanisms are supported out-of-the-box by Discount Ninja. This means there is no need to implement custom logic in most cases. |
In case the currency conversion mechanism of your theme (or supporting app) is not automatically triggered, you can publish this event after the theme has updated prices to let Discount Ninja know that it should re-render prices. |
Syntax
Collection products updated
Details
Event |
|
Direction | This event is subscribed to by the app. To publish it, use the following syntax: |
Behavior | When collection products are added, removed or updated the app must recalculate the discounted prices and render the price. |
Most mechanisms used to update collections are supported out-of-the-box by Discount Ninja. This means there is no need to implement custom logic in most cases. | |
In case the app is not automatically detecting a situation where collection products are updated, you can publish this event after the theme has updated the products to let Discount Ninja know that it should re-render prices. |
Syntax
Variant changed
Details
Event |
|
Direction | This event is subscribed to by the app. To publish it, use the following syntax: |
Behavior | When a different variant is selected, the app must recalculate the discounted price and render the price. |
Most mechanisms used to change variants are supported out-of-the-box by Discount Ninja. This means there is no need to implement custom logic in most cases. | |
In case the app is not automatically detecting a situation where the product variant is changed, you can publish this event to let Discount Ninja know that it should re-render prices. | |
Parameters |
|
Syntax
Drawer cart opened
Details
Event |
|
Direction | This event is subscribed to by the app. To publish it, use the following syntax: |
Behavior | When a drawer cart is opened (or updated) the app may need to render discounted prices. |
In case the app is not automatically detecting a situation where the drawer cart is opened, you can publish this event to let Discount Ninja know that it should re-render prices. |
Syntax
Entitlements calculated
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the (discounted) cart and the associated entitlements have been calculated. |
This event is published when the page is loaded and is published again when the content of the cart changes or the set of applied promotions changes. | |
Event properties | The event data contains an array with Entitlement info objects. Each object documents if the user is entitled to a gift for a gift offer. |
Syntax
Promotion code added
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the a promotion code is redeemed using the promotion code field. |
Event properties | The event data contains:
- |
Syntax
Promotion code removed
Details
Event |
|
Direction | This event is published by the app. You can optionally subscribe to it. |
Behavior | Published when the a promotion code is removed using the promotion code field. |
Event properties | The event data contains:
- |
Syntax
Last updated