When running inside a WebView, the LoyaltyLion JS SDK exposes additional APIs the parent mobile app can use to trigger updates and pass information

WebView APIs can be used by executing JavaScript inside the WebView and calling loyaltylion.webview.methodName, where methodName is one of the documented API methods below.

Interacting with the JS API is typically not required for a basic WebView setup, but is often used to support advanced features

Unless otherwise specified, you must wait for the LoyaltyLion SDK to report that it has finished loading before using WebView APIs. You can do this by listening to the sdkReady event.

setShopifyCartState

loyaltylion.webview.setShopifyCartState

This can be used to inform the LoyaltyLion SDK about the current Shopify cart state, which is needed for some functionality. For example, free product rewards often require a cart to contain a paid item before they can be redeemed, so the SDK needs to know about the cart first.

setShopifyCartState expects a single argument, which should be a JSON object representing the cart state. All fields can be retrieved from the Storefront API Cart object.

Properties

id
string
required

The ID of the cart

totalCost
string
required

The total amount of the customer to pay, as a decimal money amount. From cost.totalAmount.amount on the Storefront API Cart object.

lines
object[]
required

An array of objects describing the current lines on the cart

Example

loyaltylion.webview.setShopifyCartState({
  id: 'gid://shopify/Cart/Z2NwLWV1cm9',
  totalCost: '79.99',
  lines: [
    {
      id: 'gid://shopify/CartLine/19fdf4cb-5495-4c2f-83d0-40c6a4898f5e',
      quantity: 1,
      totalCost: '79.99',
      merchandiseId: 'gid://shopify/ProductVariant/600',
      productId: 'gid://shopify/Product/100',
      attributes: {},
    },
    {
      id: 'gid://shopify/CartLine/90ce960d-237e-4a02-bc28-8e4c80c112c4',
      quantity: 1,
      totalCost: '0.00',
      merchandiseId: 'gid://shopify/ProductVariant/500',
      productId: 'gid://shopify/Product/200',
      attributes: {
        __lion_sfp_id: '40c6a4898',
      },
    },
  ],
})