Skip to main content
Loveful’s storefront script (loveful.js) is loaded by the Loveful app embed with defer. It reads the page’s state from the HTML the embed prints, exposes a small bus on window.loveful, and defines the web components.
The global is always lowercase: window.loveful. It exists only while the app embed is on.

Waiting for the bus

Loveful fires loveful:ready on document once the bus is set up and, for a shopper who has just signed in, their guest items have moved to their account. Your script may load before or after Loveful’s, so check for the bus first and listen for the event otherwise:
If the app embed is turned off, neither happens and the callback never runs. loveful.ready resolves once a shopper who has just signed in has had their guest items moved to their account. For guests it resolves straight away. The examples below assume const loveful = window.loveful inside such a callback.

Properties

LovefulSnapshot

The shape of a saved item in snapshots and in item_added events:
A signed-in shopper’s items saved before this page load are in state but not in snapshots: their details were rendered by Liquid, not sent to the script.

Methods

The bus has no add, remove or toggle method. Saving and removing are done by Loveful’s elements: render a <loveful-button> for the product and let the shopper click it, and use the remove control on wishlist cards.

productSaved

boolean
Whether any variant of the product is saved. Takes a product id as a string.

snapshot

LovefulSnapshot | null
The saved item’s snapshot, or null when the page has none for it. Accepts a string or number.

clear

Promise<boolean>
Empties one of the shopper’s lists. options.list is the list number, 1 (the default list, and a guest’s only one) to 10. Resolves false when the request fails, or when a guest asks for a list other than 1. Clearing list 1 fires item_removed for each item, then one list_cleared.

t

string
A storefront string from i18n, or fallback when the key is missing or empty.
Placeholders are left for you to fill: %count%, %title% or %email% in the defaults, or {{ count }} and the like in the merchant’s overrides:

publishAnalytics

void
Publishes a custom event to Shopify’s customer events (Shopify.analytics.publish), for custom pixels to subscribe to. Does nothing where that API is unavailable.

Share API

Each method calls Loveful through the store’s app proxy. getToken, resetToken and email need a signed-in shopper; getList works for anyone. share_url is built on the shopper’s current domain and language root, with the token in the loveful_token query parameter.

share.getToken

Promise<{ token, share_url }>
The default list’s share link, made on first use. Rejects when the request fails.

share.resetToken

Promise<{ token, share_url }>
A new share link. The old one stops working. Rejects when the request fails.

share.getList

Promise<LovefulSharedListResponse>
A shared list by its token. Rejects with loveful.share.getList: not_found when the token is unknown or was reset.

share.email

Promise<LovefulShareEmailResult>
Emails the wishlist to one address, in the shopper’s language. Never rejects.
<loveful-share-link>’s email form shows each one in the merchant’s words: loveful.share_email_limit_recipient, loveful.share_email_limit, loveful.share_email_off and loveful.share_email_failed.

Bus events

window.loveful is an EventTarget. It dispatches CustomEvents whenever the saved state changes, in this tab or in another tab of the same store (changes are relayed between tabs with a BroadcastChannel named loveful). tab_id is the tab the change was made in: compare it with loveful.tabId to tell local changes from relayed ones.

item_added

{ variant_id: string, snapshot: LovefulSnapshot, tab_id: string }
A variant was saved. state, snapshots and productOf are already updated.

item_removed

{ variant_id: string, tab_id: string }
A variant was removed. Also fired for each item when the default list is cleared.

list_cleared

{ variant_ids: string[], tab_id: string }
The default list was emptied, after an item_removed for each of variant_ids.

Document events

loveful:ready

Fired on document once, with detail: { loveful }. See Waiting for the bus.

Share events

<loveful-share-link> dispatches bubbling CustomEvents on itself, so they can be caught on document:

Events Loveful listens for

The save button and the saved count follow the theme’s variant picker. They listen on document for:
  • shopify:product:select, Shopify’s standard storefront event: the variant comes from the event’s promise, which resolves with { variant }.
  • variant:update, variant:change and change: the variant id is read from event.detail.variant.id or event.detail.resource.id (a GID or a number), or else from the ?variant= URL parameter a tick later.
A theme with a custom picker can dispatch one of these to keep Loveful in step:

Shopify customer events

Loveful publishes these through Shopify.analytics.publish, so a custom pixel can subscribe to them. They are published only in the tab where the change was made.
Custom pixel

Data attributes on cards

Wishlist cards (li.loveful-card) carry data-variant-id and data-handle. While a card’s options are being changed it also carries data-swap-to with the new variant id.

Storage

Opening a shared wishlist with items sets the hidden cart attribute _loveful_share to the share token (or <token>:<send id> when opened from a share email), so an order can be traced back to the share.