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
la:dn:api:ready
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
la:dn:promotions:loaded
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
la:dn:product:discount:calculated
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
Syntax
Cart updated
Details
Event
la:dn:cart:updated
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
Syntax
Cart mutations processing
Details
Event
la:dn:cart-mutations:processing
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.
Syntax
Cart mutations processed
Details
Event
la:dn:cart-mutations:processed
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.
Syntax
Trigger checkout
Details
Event
la:dn:checkout:initiate
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.
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
la:dn:money:convert
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
la:dn:collection:updated
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
la:dn:variant:changed
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
variantId (number)
: the id of the selected variant
Syntax
Drawer cart opened
Details
Event
la:dn:drawercart:opened
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
la:dn:entitlements:calculated
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
Syntax
Promotion code added
Details
Event
la:dn:promotioncode:added
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:
- promotionCode
: the promotion code that is applied
- success
: a boolean indicating that the code was added successfully
- manual
: a boolean indicating if the code was added manually or by the system
Syntax
Promotion code removed
Details
Event
la:dn:promotioncode:removed
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:
- promotionCode
: the promotion code that is removed
- success
: a boolean indicating that the code was removed successfully
Syntax
Last updated