API ReferenceGuides
Log In
API Reference

Header Icon

Add a wishlist icon with live count badge to your store header using the Swym JS SDK.

Header Icon (Count Badge)

A wishlist icon in your store header that shows the count of wishlisted items and updates in real-time.

Liquid — Add to your theme header

Place this in your header snippet (e.g., header.liquid or sections/header.liquid):

{% comment %} Swym Wishlist Header Icon — SDK-Only Mode {% endcomment %}
<a href="/pages/wishlist" class="swym-header-wishlist" aria-label="Wishlist">
  {% comment %} Add your icon here (heart SVG, image, etc.) {% endcomment %}
  <span id="swym-header-count" style="display:none;">0</span>
</a>

JavaScript

(function() {
  'use strict';

  function initHeaderIcon(swat) {
    updateCount(swat);

    // Listen for wishlist changes to update count in real-time
    if (swat.evtLayer) {
      swat.evtLayer.addEventListener('sw:addedtowishlist', function() {
        updateCount(swat);
      });
      swat.evtLayer.addEventListener('sw:removedfromwishlist', function() {
        updateCount(swat);
      });
    }
  }

  function updateCount(swat) {
    swat.fetch(function(products) {
      var countEl = document.getElementById('swym-header-count');
      if (!countEl) return;

      var count = products ? products.length : 0;
      countEl.textContent = count;
      countEl.style.display = count > 0 ? '' : 'none';
    });
  }

  window.SwymCallbacks = window.SwymCallbacks || [];
  window.SwymCallbacks.push(initHeaderIcon);
})();

How It Works

  1. On SDK ready, swat.fetch() counts wishlisted products → sets badge text
  2. Badge is hidden when count is 0, shown when > 0
  3. Subscribes to sw:addedtowishlist and sw:removedfromwishlist events on swat.evtLayer
  4. On each event, re-fetches count so the badge stays accurate

Styling

The <span> is hidden/shown via inline display. Style it as a badge overlay on your icon.

Gotcha: theme CSS stretching the badge. Many themes have rules like .header__icon span { height: 100% } that will stretch your count badge into a tall oval. Fix this by setting an explicit height on your count element (e.g., height: 1.7rem !important). Match your theme's cart count bubble styling — inspect .cart-count-bubble in DevTools to see the exact dimensions and positioning.

Events Available

EventFires when
sw:addedtowishlistProduct is added to any wishlist
sw:removedfromwishlistProduct is removed from any wishlist
sw:addedtocartProduct is added to cart from wishlist