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.
export function WishlistButton({ productId }: { productId: number }) {
return (
<button onClick={() => window.salla?.wishlist.toggle(productId)}>
Save
</button>
);
}Methods
| Method | Parameters | Does |
|---|---|---|
add | id or { id } | Saves the product |
remove | id or { id } | Removes the product |
toggle | id | Removes the product when it is saved, saves it otherwise |
has | id | true when the product is saved. No request. |
getWishlistIds | none | The ids of the saved products. No request. |
share | productIds, 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
| Event | Fires when | Arguments |
|---|---|---|
wishlist::before.add.item / wishlist::before.remove.item | Before add / remove | { product_id, preventDefault } |
wishlist::added / wishlist::addition.failed | add | response or error, product id |
wishlist::removed / wishlist::removing.failed | remove | response or error, product id |
wishlist::before.share | Before share | { product_ids, select_all, preventDefault } |
wishlist::shared / wishlist::sharing.failed | share | response or error, product ids |
wishlist::storage.synced | The saved ids were read from the server | none |
Gotchas
toggledecides from the ids saved in the browser. It has no events of its own: it fires those ofaddorremove.- A
beforehandler that throws does not stop the action. UsepreventDefault.