wishlist

Add, remove, toggle and share wishlist products, and check what is already saved.

Use Salla.wishlist to change the customer's wishlist. In components, the useWishlist hook wraps it and keeps your hearts in sync, so reach for that first.

app/components/product/WishlistButton.tsx
export function WishlistButton({ productId }: { productId: number }) {
  return (
    <button onClick={() => window.salla?.wishlist.toggle(productId)}>
      Save
    </button>
  );
}

Methods

MethodParametersDoes
addid or { id }Saves the product
removeid or { id }Removes the product
toggleidRemoves the product when it is saved, saves it otherwise
hasidtrue when the product is saved. No request.
getWishlistIdsnoneThe ids of the saved products. No request.
shareproductIds, selectAll?Creates a share link for those products, or for all of them with selectAll

has and getWishlistIds read the ids saved in the browser. For a logged-in customer the SDK fills them from the server when the page starts, then fires wishlist::storage.synced.

Guests

The wishlist needs a logged-in customer. For a guest, add, remove and toggle open the login popup and reject with a "must log in" message. After the customer logs in, the SDK finishes the add or remove by itself, even when the login reloaded the page.

Stop or change an action

Before it sends a request, the SDK fires a before event with an object you can change. Set preventDefault to true to cancel:

Salla.event.on('wishlist::before.add.item', (data: { product_id: number; preventDefault: boolean }) => {
  if (data.product_id === 1234) data.preventDefault = true; // this product cannot be saved
});

wishlist::before.share passes { product_ids, select_all, preventDefault }, and the SDK uses your changed ids.

Events

EventFires whenArguments
wishlist::before.add.item / wishlist::before.remove.itemBefore add / remove{ product_id, preventDefault }
wishlist::added / wishlist::addition.failedaddresponse or error, product id
wishlist::removed / wishlist::removing.failedremoveresponse or error, product id
wishlist::before.shareBefore share{ product_ids, select_all, preventDefault }
wishlist::shared / wishlist::sharing.failedshareresponse or error, product ids
wishlist::storage.syncedThe saved ids were read from the servernone

Gotchas

  • toggle decides from the ids saved in the browser. It has no events of its own: it fires those of add or remove.
  • A before handler that throws does not stop the action. Use preventDefault.

See also

On this page