> ## Documentation Index
> Fetch the complete documentation index at: https://docs.loveful.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Code examples

> Copy-paste Liquid, HTML, JavaScript and CSS for common Loveful customisations.

Each example needs the Loveful app embed on. Where Liquid reads Loveful's metafields, replace `app--1234567` with the namespace in your `loveful-wishlist` snippet (Settings > Storefront fills it in).

## Save button in a quick view

Quick views are usually fetched and inserted after the page loads. The button sets itself up when it's inserted, so render it like any other markup:

```liquid snippets/quick-view.liquid theme={null}
{%- assign variant = product.selected_or_first_available_variant -%}
<loveful-button
  variant-id="{{ variant.id }}"
  data-product-id="{{ product.id }}"
  data-handle="{{ product.handle }}"
  data-title="{{ product.title | escape }}"
  data-image="{{ product.featured_image | image_url: width: 400 }}"
  data-price="{{ variant.price | divided_by: 100.0 }}"
  data-currency="{{ cart.currency.iso_code }}"
  data-available="{{ variant.available }}"
  role="button"
  tabindex="0"
>Save to wishlist</loveful-button>
```

If the quick view's variant picker doesn't fire a standard variant event, tell Loveful when it changes:

```js theme={null}
picker.addEventListener("change", () => {
  document.dispatchEvent(
    new CustomEvent("variant:change", { detail: { variant: { id: picker.value } } }),
  );
});
```

## Save button beside add to cart

Inside the theme's product form, sized like its buttons:

```liquid snippets/buy-buttons.liquid theme={null}
<div class="product-form__buttons" style="display: flex; gap: 0.5rem">
  <button type="submit" class="product-form__submit button">Add to cart</button>
  <loveful-button
    variant-id="{{ product.selected_or_first_available_variant.id }}"
    data-product-id="{{ product.id }}"
    data-handle="{{ product.handle }}"
    data-title="{{ product.title | escape }}"
    role="button"
    tabindex="0"
    style="--loveful-btn-width: auto; flex: none"
  >Save to wishlist</loveful-button>
</div>
```

## Show a save button only once Loveful is running

Render it `hidden`: Loveful removes `hidden` when it sets the button up, so it never shows while the app embed is off.

```html theme={null}
<loveful-button hidden variant-id="44012345678901" data-product-id="8012345678901" data-handle="linen-shirt" role="button" tabindex="0">Save to wishlist</loveful-button>
```

## "Saved by" on product cards

The product's saved count, read straight from its metafield. It doesn't follow the shopper's own saves; for that, use [`<loveful-saved-count>`](https://docs.loveful.app/developers/web-components#loveful-saved-count).

```liquid snippets/card-product.liquid theme={null}
{%- assign saved_by = card_product.metafields['app--1234567'].saved_count.value | default: 0 -%}
{%- if saved_by >= 5 -%}
  <span class="badge">Saved by {{ saved_by }}</span>
{%- endif -%}
```

## Wishlist link with a translated label

```liquid theme={null}
<a href="{{ pages.wishlist.url | default: '/pages/wishlist' }}" aria-label="{{ 'general.wishlist' | t | default: 'Wishlist' }}">
  <loveful-header-icon aria-label="{{ 'general.wishlist' | t | default: 'Wishlist' }}"></loveful-header-icon>
</a>
```

## "My account" wishlist count

```liquid templates/customers/account.liquid theme={null}
{%- assign loveful_count = customer.metafields['app--1234567'].refs_1.value | size -%}
<a href="/pages/wishlist">
  Wishlist (<loveful-count data-hide-at-zero="false">{{ loveful_count }}</loveful-count>)
</a>
```

## Clear wishlist button

```html theme={null}
<button type="button" id="clear-wishlist">Clear wishlist</button>

<script>
  document.getElementById("clear-wishlist").addEventListener("click", async () => {
    if (!window.loveful || !confirm("Remove everything from your wishlist?")) return;
    const ok = await window.loveful.clear();
    if (!ok) alert("Couldn't clear your wishlist. Try again.");
  });
</script>
```

The wishlist's cards and every count update from the `item_removed` events `clear()` fires.

## Share link on the account page

Your own button, with Loveful doing the work:

```liquid templates/customers/account.liquid theme={null}
<loveful-share-link hidden data-path="/pages/shared-wishlist">
  <button type="button" class="button" data-loveful-share-copy>Copy my wishlist link</button>
</loveful-share-link>
```

Or straight from the bus:

```js theme={null}
const { share_url } = await window.loveful.share.getToken();
await navigator.share?.({ title: "My wishlist", url: share_url });
```

## A shared list as plain links

```html templates/page.shared-wishlist.liquid theme={null}
<loveful-shared-list>
  <p>Loading wishlist…</p>
</loveful-shared-list>

<style>
  .loveful-shared-list { grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)); }
  .loveful-shared-list__price { font-weight: 600; }
</style>
```

## Add-to-wishlist toast

```js assets/wishlist-toast.js theme={null}
const onLoveful = (callback) => {
  if (window.loveful) window.loveful.ready.then(() => callback(window.loveful));
  else document.addEventListener("loveful:ready", (e) => callback(e.detail.loveful), { once: true });
};

onLoveful((loveful) => {
  loveful.addEventListener("item_added", (e) => {
    if (e.detail.tab_id !== loveful.tabId) return;
    const toast = document.createElement("div");
    toast.className = "wishlist-toast";
    toast.setAttribute("role", "status");
    toast.textContent = loveful.t("loveful.added_to_wishlist", "Added to your wishlist");
    document.body.append(toast);
    setTimeout(() => toast.remove(), 3000);
  });
});
```

## Wishlist saves in GA4 from a custom pixel

```js Custom pixel theme={null}
analytics.subscribe("loveful:item_added", (event) => {
  const { variant_id, product_id } = event.customData;
  gtag("event", "add_to_wishlist", {
    items: [{ item_id: product_id, item_variant: variant_id }],
  });
});
```

## Card hearts that match the theme

Set on the element, so the theme's colour scheme variables resolve on the card:

```css theme={null}
loveful-button[appearance="icon"] {
  --loveful-card-bg: transparent;
  --loveful-card-color: rgb(var(--color-foreground));
  --loveful-card-saved-color: #c8102e;
  --loveful-card-size: 20px;
}
```

## Pill-shaped save button

```css theme={null}
loveful-button:not([appearance="icon"]) {
  --loveful-btn-radius: 999px;
  --loveful-btn-padding: 0.625rem 1.5rem;
  --loveful-btn-saved-bg: #c8102e;
  --loveful-btn-saved-border: #c8102e;
  --loveful-btn-saved-fg: #fff;
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.