Functions

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.

Cache

Clear

Async

Yes

Function

await discountNinja.api.cache.clear()

Clears all session and local storage data used by the script. Also clears the content of the indexedDb used by the script.

Type

Public

This function is intended for use in troubleshooting sessions.

Syntax

//Clear cache
if (discountNinja) await discountNinja.api.cache.clear();

Cart

Add

Async

Yes

Function

await discountNinja.api.cart.add(variant: number, quantity: number, properties?: Record<string, string>, sellingPlanId?: number)

Adds a variant to the cart. Note: this is an asynchronous function; the result must be awaited.

Type

Public

This function is intended for use in integration code by third parties.

Parameters

variant (number): the variant id

quantity (number): the number of items to add

properties? (Record<string, string>): an optional object with key/value pairs of line item properties

sellingPlanId? (number): an optional selling plan (to allow for recurring purchases based on a subscription)

Syntax

Content

Async

No

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.

Return value

object | null Returns the Shopify cart object. Returns null if no cart is available.

Syntax

Prefill

Async

Yes

Property

await discountNinja.api.cart.prefill(variants?: string[], redirectUrl?: string)

Can be used to prefill a cart with specific variants. Typically used to create a link to a prefilled, discounted cart. Cf. https://support.discountninja.io/en/articles/5194395-how-to-build-a-prefilled-discounted-cart

Type

Public

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

Syntax: example 1

This script can be used on a cart template to automatically prefill the cart based on the query parameters as explained here: ttps://support.discountninja.io/en/articles/5194395-how-to-build-a-prefilled-discounted-cart

Syntax: example 2

Alternatively, create a variants array manually and then

Checkout

Is discounted

Async

Yes

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

Add pipeline rule

Async

No

Function

discountNinja.api.checkout.addPipelineRule()

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

Discount code

Add

Async

Yes

Function

await discountNinja.api.discountCode.add(discountCode: string)

Adds a discount code or promotion code to the Discount Ninja promotion code field programmatically.

Type

Public

This function is intended for use in integration code by third parties.

Parameters

discountCode (string): the discount code to add

Syntax

Remove

Async

Yes

Function

await discountNinja.api.discountCode.remove(discountCode: string)

Removes a discount code or promotion code from the Discount Ninja promotion code field programmatically.

Type

Public

This function is intended for use in integration code by third parties.

Parameters

discountCode (string): the discount code to remove

Syntax

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 | null Returns an object representing the current cart, with the applicable Discount Ninja offers applied. Note that amounts returned are in cents, not in dollars.

Syntax

Entitlements

Property

discountNinja.api.entitlements

Lists the entitlements that apply based on the available offers. The array contains one item for each available offer. It details the prerequisites that were found in the cart, the discount amount, the target products and the entitled line items that were found in the cart.

Type

Public

This function is intended for use in integration code by third parties..

Return value

object[] | null Returns an array of Entitement info objects.

Syntax

Events

Subscribe

Async

No

Function

discountNinja.api.events.subscribe(eventName: string)

Provides instructions on how to subscribe to an event with a given name. Note: this function does not, itself, subscribe to the event.

See the list of available events.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Publish

Async

No

Function

discountNinja.api.events.publish(eventName: string)

Publishes an event to the PriceRuleEngine.

See the list of available events.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Help

Async

No

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.

Syntax

Line item

Get updated key

Async

No

Function

discountNinja.api.lineItem.getUpdatedKey(key: string)

Returns the updated key associated with a cart line item. A key can be updated by Shopify's back-end when properties are added, a selling plan changes or a discount is added. Cf. https://support.discountninja.io/en/articles/9109023-fix-network-requests-to-cart-js

Type

Public

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

Offers

All

Async

No

Function

discountNinja.api.offers.all

Lists all the active offers loaded by the app.

Type

Internal

This function returns an internal object. Do not rely on the properties of this object in your integration code as they may be removed or renamed in future versions.

Return value

object[] An array of offer objects, representing the offers that are applicable in the current context.

Syntax

Get

This function is used to look up an offer that is available in the browser's cache. To load a promotion that is not available to the browser, use the Promotion Trigger function or the Discount Code Add function instead.

Async

No

Function

discountNinja.api.offers.get(token: string)

Finds a specific offer in the list of all the active offers loaded by the app.

Type

Internal

This function returns an internal object. Do not rely on the properties of this object in your integration code as they may be removed or renamed in future versions.

Parameters

token (string): the token of the offer to find

Return value

object | null Anoffer object if the offer is found, otherwise null.

Syntax

Promotions

All

Function

discountNinja.api.promotions.all

Lists all the active promotions loaded by the app.

Type

Internal

This function returns an internal object. Do not rely on the properties of this object in your integration code as they may be removed or renamed in future versions.

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.

Syntax

Get

This function is used to look up a promotion that is available in the browser's cache. To load a promotion that is not available to the browser, use the Promotion Trigger function or the Discount Code Add function instead.

Function

discountNinja.api.promotions.get(token: string)

Finds a specific promotion in the list of all the active promotions loaded by the app.

Type

Internal

This function returns an internal object. Do not rely on the properties of this object in your integration code as they may be removed or renamed in future versions.

Parameters

token (string): the token of the promotion to find

Return value

object | null A promotion object if the promotion is found, otherwise null.

Syntax

Trigger

Async

No

Function

discountNinja.api.promotions.trigger(tokens: string)

Triggers a private promotion programmatically.

Use this as a programmatic alternative to using a discount link. To trigger a promotion based on a promotion code, use the Discount Code Add function instead.

Type

Public

This function is intended for use in integration code by third parties.

Parameters

tokens (string | string[]): the token(s) of the promotion(s) to trigger

Syntax

Strikethrough Pricing

Apply

Async

Yes

Function

await discountNinja.api.strikethroughPricing.apply()

Triggers a refresh of all strikethrough pricing programmatically. This is typically not required since the app automatically detects situations where the strikethrough pricing must be updated.

For an event based approach, use this event instead.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Before render

Async

No

Function

discountNinja.api.strikethroughPricing.setBeforeRender(async (input: Object) => string)

Discount Ninja ships with its own internal rendering function wired up as the “before render” callback. This function allows the user to override this callback with a custom callback.

Parameters

async (input: Object) => string) A function that receives an input object and returns a string.

priceData: Object An input object that can be used to decide if the rendered text should be updated.

string The HTML that should be rendered. To render the HTML proposed by the internal rendering function, simply return input.defaultHtml

This is the shape of the inputobject:

Return value

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Widgets

Render

Async

Yes

Function

await discountNinja.api.widgets.render()

Triggers a refresh of all widgets (including strikethrough pricing) programmatically. This is typically not required since the app automatically detects situations where the widgets must be updated.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Widgets > Promotion Code Field

Toggle

Async

Yes

Function

await discountNinja.api.widgets.promotionCodeField.toggle()

Expands or collapses the promotion code field widget.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Async

No

Function

discountNinja.api.widgets.promotionCodeField.showFooterInstructions()

discountNinja.api.widgets.promotionCodeField.hideFooterInstructions()

discountNinja.api.widgets.promotionCodeField.toggleFooterInstructions()

Expands or collapses the instructions section in the footer of the promotion code field.

Type

Public

This function is intended for use in integration code by third parties.

Syntax

Last updated

Was this helpful?