Functions and properties available on the discountNinja.api namespace.
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.
Cart
Content
Property
discountNinja.api.cart.content
Can be used to get access to an object that represents the content of the cart, including any discounts applied by Shopify's backend.
Type
Public
This function is intended for use in integration code by third parties.
This function is intended for use in integration code by third parties.
Parameters
variants?: string[]: a comma-separated list of the variant id of the product variants you want to add to the cart; for each variant you'll need to specify the quantity.
This parameter is optional. If it is omitted, the app will look for a query parameter named variants instead. If neither is available the prefill method will not execute.
redirectUrl?: string: define which page should be loaded once the cart is prefilled
This parameter is optional. If it is omitted, the app will look for a query paramer named redirectUrl instead. If neither is available the default value is "/cart".
functiontest() {document.addEventListener("la:dn:promotions:loaded",function() { // Prefilling the cart based on the data in the query parameters// The query parameters may look as follows:// .../?variants=1234567x1,2345678x2&redirectUrl=/cartdiscountNinja.api.cart.prefill();// Add a quantity of 1 of the variant with id 1234567x1// and a quantity of 2 of the variant with id 2345678x2// then, redirect to the home pageconstvariants= [];variants.push('1234567x1');variants.push('2345678x2');discountNinja.api.cart.prefill(variants,'/'); }); }
Checkout
Is discounted
Function
await discountNinja.api.checkout.isDiscounted()
Can be used to check if any of the promotions apply to the cart and result in a product, order or shipping discount.
Type
Public
This function is intended for use in integration code by third parties.
Return value
Promise<boolean> Returns a promise which resolves to a boolean.
The return value indicates if the app will instruct Shopify to apply discounts at checkout. Additionally, when this function returns true, the script will attempt to take over the checkout process when the cart form is submitted using a checkout button.
Syntax
document.addEventListener("la:dn:cart:updated",function(event) {constdiscountedCart=event.detail.data[0]; constisDiscounted=awaitdiscountNinja.api.checkout.isDiscounted();if (isDiscounted) { console.log('The cart is discounted', discountedCart);// Add logic when the cart is discounted by Discount Ninja offers }else {console.log('The cart is not discounted', discountedCart);// Add logic when the cart is not discounted by Discount Ninja offers }});
Can be used to add custom business logic that is executed when the customer click the checkout button.
Discount Ninja needs to take over the checkout process when a user clicks the checkout button to ensure the checkout is correctly discounted.
As a result, the app overrides any logic you may have associated with clicking the checkout button. To execute this logic you can add it to the pipeline of rules that is executed by the app.
Each pipeline rule is a parameterless function that returns a boolean indicating if execution should continue (true) or not (false).
Type
Public
This function is intended for use in integration code by third parties.
Return value
boolean The return value indicates if the rule was added properly.
Syntax
functioncheckoutPipelineRule1() {//Add your logic here//Return true to indicate that the customer can continue to the checkout//Return false to halt the pipelineconsole.log('Executing checkout pipeline rule 1');returntrue; }functioncheckoutPipelineRule2() {//Add your logic here//Return true to indicate that the customer can continue to the checkout//Return false to halt the pipelineconsole.log('Executing checkout pipeline rule 2');returntrue; }functionregisterCheckoutPipelineRules() {constsuccessfullyAddedRule1=discountNinja.api.checkout.addPipelineRule(checkoutPipelineRule1);constsuccessfullyAddedRule2=discountNinja.api.checkout.addPipelineRule(checkoutPipelineRule2);if (successfullyAddedRule1 && successfullyAddedRule2) {console.log('Pipeline rules successfully registered'); }else {console.error('Pipeline rules could not be registered'); }}//Check if the API is readyif (typeof discountNinja ==='undefined'||typeofdiscountNinja.api ==='undefined') {//The API is not ready, wait for itdocument.addEventListener("la:dn:api:ready",function(event) { registerCheckoutPipelineRules(); });}else {//The API was already available, no need to waitregisterCheckoutPipelineRules();}
functiontest() {constdiscountCode='ABCD';awaitdiscountNinja.api.discountCode.add(discountCode);console.log(`Added the promotion with promotion code '${discountCode}' to the cart`);}
functiontest() {constdiscountCode='ABCD';awaitdiscountNinja.api.discountCode.remove(discountCode);console.log(`Removed the promotion with promotion code '${discountCode}' from the cart`);}
Discounted cart
Content
Property
discountNinja.api.discountedCart.content
Can be used to get access to an object that represents the content of the cart, including any discounts applied by Shopify's backend.
Type
Internal
This is an internal function or property.
Use it only for debugging purposes.
Do not rely on this function or property in your integration code.
Return value
object Returns an object representing the current cart, with the applicable Discount Ninja offers applied. Note that amounts returned are in dollars, not cents.
Syntax
functiontest() {constdiscountedCart=discountNinja.api.discountedCart;console.log(`Removed the promotion with promotion code '${discountCode}' from the cart`);}
This function is intended for use in integration code by third parties.
Syntax
functiontest() {discountNinja.api.events.subscribe('cart:updated');}// Prints the following message on the console:// To subscribe to this event use the following code: // document.addEventListener("la:dn:cart:updated", function(event) { const eventData = event.detail.data[0]; console.log('Event la:dn:cart:updated was published.', eventData); });
This function is intended for use in integration code by third parties.
Parameters
key (string): the original key of the cart line item
Return value
string The updated key.
Syntax
functiontest() {for (let i =0; i <cart.items.length; i++) {constitem=cart.items[i];let key =item.key;if (discountNinja) { key =discountNinja.api.lineItem.getUpdatedKey(key); }// ... use the updated key, for example, to send requests to cart.js }});
Offers
All
Function
discountNinja.api.offers.all
Lists all the active offers loaded by the app.
Type
Internal
This is an internal function or property.
Use it only for debugging purposes.
Do not rely on this function or property in your integration code.
Return value
object[] An array of offer objects, representing the offers that are applicable in the current context.
Finds a specific offer in the list of all the active offers loaded by the app.
Type
Internal
This is an internal function or property.
Use it only for debugging purposes.
Do not rely on this function or property in your integration code.
Parameters
token (string): the token of the offer to find
Return value
object | null Anoffer object if the offer is found, otherwise null.
Syntax
document.addEventListener("la:dn:promotions:loaded",function(event) { constofferToken='ABCD';constbfcmOffer=discountNinja.api.offers.get(offerToken);if (bfcmOffer ===null) {console.log(`Offer with token ${offerToken} not found`); }else {console.log(`Offer with token ${offerToken} found`, bfcmOffer); }});
Promotions
All
Function
discountNinja.api.promotions.all
Lists all the active promotions loaded by the app.
Type
Internal
This is an internal function or property.
Use it only for debugging purposes.
Do not rely on this function or property in your integration code.
Return value
object[] An array of promotion objects, representing the promotions that are applicable in the current context. The promotion object provides information about the trigger used and the list of offers associated with the promotion.
<ahref='javascript:triggerCustomPromotion()'>Unlock the BFCM promo!</a><buttononclick='triggerCustomPromotion()'>Unlock the Cyber Monday offer!</button>
Help
Function
discountNinja.api.help()
Prints a link to this page on the console.
Type
Public
This function is intended for use in integration code by third parties.