Velo Tutorial: Adding a Wishlist to a Wix Stores Site
You can also use the built-in wishlist feature in the Wix Editor. Learn more here.

Overview
- A My Wishlist page to the Member Pages group with a repeater that displays the member's wishlist. It also has a group of elements that are displayed if the member's wishlist is empty.
- A products-wishlist collection with 2 fields:
- User Id: This stores the ID of the currently signed-in member.
- Product: This is a reference field that points to the product the member added to their wishlist.
- Icons on the Shop Product Page that show if a product is already in the member's wishlist. We also use these icons as a toggle to let the member add or remove the item from their wishlist.
- When the Product Page loads, check to see if the current product is associated with the member in the products-wishlist collection and display the corresponding icon.
- Set up the icons so that when they are clicked they either add or remove the product from the products-wishlist collection.
- When the My Wishlist page loads, query the products-wishlist collection for all the products associated with the current member and display them in the repeater. Hide the repeater if there are no associated products.
- Prompt the user to log in if they aren't and they try to add or remove an item from their wishlist.
Step 1: Set up the Site


- Product: A reference to the product the member added.
- User Id: The ID of the user who adds a product to their wishlist.

In our example we made User Id the Primary field and deleted the Title field from the collection.
Step 2: Set up the Shop Product Page
- A group of elements that prompt the user to log in if they need to.
- Heart icons that tell the user if the product is in their wishlist. The icons also work as a toggle to add or remove the item from their wishlist.
Note: In the image below the two icons are displayed side-by-side. On the actual site, they're overlapping and the code controls which is displayed.

Step 3: Create and Set Up the My Wishlist Page
- Click the Pages button, then Member Pages, then + Add a Member Page.

- Make the My Wishlist page a Private Page.

- Then rename the new member page by accessing the page's settings. Change the name to My Wishlist.

- Access and edit the My Wishlist page by clicking the Page Code button in the Velo Sidebar, then My Wishlist in the Member's Area (members) dropdown.

- A container box that contains a text box and a button labelled Go Shopping. The container box is set to "Collapsed on load" in the Properties & Events panel and is only displayed if the member's wishlist is empty or if they aren't signed in.
- A repeater that will display the wishlist. Because the container box is set to be collapsed if there's a wishlist, the repeater moves up to take its space when there is a wishlist.

Step 4: Create the Add to Wishlist Function on the Product Page
1import wixData from 'wix-data';
2import wixUsers from 'wix-users';
3
4let product;
5let user = wixUsers.currentUser;
6
7$w.onReady(async function () {
8 product = await $w('#productPage1').getProduct();
9});
10
11async function addToWishlist() {
12 let wishListItem = {
13 product: product._id,
14 userId: user.id
15 };
16
17 let result = await wixData.insert("Wishlist", wishListItem);
18}
Understanding the Code
Lines 1-2: Import the modules we need to work with Wix Data and Wix Users libraries.
Line 4: Define the product
variable.
Line 5: Set the user
variable to the current user.
Lines 7-9: Get the current product from the Product page.
Lines 11-15: Create the addToWishList
function. Build the wishListItem
object. This object contains the product ID for the current product and the ID of the current user.
Line 17: Insert the wishListItem
object to the products-wishlist
collection.
Identifiers you may need to change based on your site's elements
#productPage1
wishlist
Step 5: Create the notInWishList_click Function on the Product Page
notInWishList_click
is the function that runs when the empty heart icon is clicked. It hides the empty heart icon, displays the full icon, and then calls the addToWishList
function we defined in step 4 that handles adding the product to the member's wishlist.
1export function notInWishList_click(event, $w) {
2 if (user.loggedIn) {
3 addToWishlist();
4 $w('#notInWishList').hide('fade', {duration: 100});
5 $w('#inWishList').show('fade', {duration: 100});
6 }
7 else
8 $w('#loginMessage').show();
9}
Understanding the Code
Line 2: Check to see if the current user is logged in.
Line 3: If the user is logged in, call addToWishList
, which adds the current product to the user's wishlist.
Lines 4-5: Hide the empty heart and show the full heart, indicating to the user that the product is now in their wishlist.
Lines 7-8: Prompt the user to log in if they aren't.
Identifiers you may need to change based on your site's collections
#notInWishList
#inWishList
#loginMessage
Step 6: Create the removeFromWishList Function on the Product Page
1async function removeFromWishlist() {
2 let wishListResult = await wixData.query("products-wishlist")
3 .eq("product", product._id)
4 .eq("userId", user.id)
5 .find();
6
7 if (wishListResult.length > 0) {
8 $w('#notInWishList').show('fade', {duration: 100});
9 $w('#inWishList').hide('fade', {duration: 100});
10 await wixData.remove("Wishlist", wishListResult.items[0]._id)
11 }
12}
Understanding the Code
Lines 2-5: Run a query on the products-wishlist
collection for the current product for the signed in member.
Line 7: Check that the item exists in products-wishlist
for the current member (results of the query are greater than 0).
Lines 8-9: Display the empty heart icon and hide the full heart icon.
Line 10: Call wixData.remove
to remove the current product from the products-wishlist
collection.
Identifiers you may need to change based on your site's collections
wishlist
product
userID
#notInWishlist
#inwishlist
Step 7: Create the inWishList_click Function on the Product Page
inWishList_click
is the function that runs when the user clicks the full heart icon and wants to remove a product from their wishlist. It checks to see that the user is logged in and then calls the removeFromWishlist
function we defined in step 6.
1export function inWishList_click(event, $w) {
2 if (user.loggedIn)
3 removeFromWishlist();
4}
Understanding the Code
Line 2: Check if the current user is logged in.
Line 3: Call removeFromWishlist
to remove the current product from the member's wishlist and change the displayed icons.
Step 8: Create the checkWishlist Function on the Product Page
checkWishlist
is the function that runs when the Product Page loads. It checks to see if the current product is in the member's wishlist. Here also the appropriate icon is displayed depending on the product's wishlist status. If the current user isn't logged in, the empty heart icon is also displayed.
1async function checkWishlist() {
2 if (wixUsers.currentUser.loggedIn) {
3 let wishListResult = await wixData.query("products-wishlist")
4 .eq("product", product._id)
5 .eq("userId", user.id)
6 .find();
7
8 if(wishListResult.items.length > 0) {
9 $w('#inWishList').show('fade', {duration: 100});
10 }
11 else {
12 $w('#notInWishList').show('fade', {duration: 100});
13 }
14 }
15 else {
16 $w('#notInWishList').show('fade', {duration: 100});
17 }
18}
Understanding the Code
Line 2: Check that the current user is logged in.
Lines 3-6: Query the products-wishlist
collection for the current product and the signed in member.
Line 8: Check that the product exists in the wishlist for the current member (results of the query are greater than 0).
Lines 9-12: If the product is in the member's wishlist, display the full heart icon. If not, display the empty heart icon.
Lines 15-16: If the current user isn't logged in, display the empty heart icon.
1checkWishlist();
Identifiers you may need to change based on your site's elements
wishlist
product
userId
#inWishList
#notInWishList
Step 9: Create the loginMessageClick Function on the Product Page
When we set up our site we added a group of elements to the Product Page that prompt the user to log in if they aren't already. Now we need to create the loginMessageClick
function that displays the login prompt if the user clicks an element in that group.
The loginMessageClick
function displays the login prompt to the user, and we want it to run when a user clicks anywhere in the group. We can't use the Properties & Events panel to add a click event handler to a group, so instead we'll create that event handler directly in the code. We'll create this event handler in the onReady
function for the Product Page.
1async function loginMessageClick() {
2 let options = {"mode": "login"};
3 $w('#loginMessage').hide();
4 await wixUsers.promptLogin(options);
5}
Understanding the Code
Line 2: Create an options
object that will be sent to the promptLogin
function. This sets the login to prompt the user to log in as opposed to sign up.
Line 3: Hide the loginMessage
group of elements.
Line 4: Call wixUsers.promptLogin
with the options
object.
1$w('#loginMessage').onClick(loginMessageClick);
Understanding the Code
Line 1: Create an onClick
event for the loginMessage
group of elements that calls the loginMessageClick
function.
Identifiers you may need to change based on your site's elements
#loginMessage
Step 10: Create the loadWishlist Function on the My Wishlist Page
First let's import the modules we need to work with Wix Data, Wix Users, and Wix Location libraries. This code needs to be at the top of the page.
1import wixData from 'wix-data';
2import wixUsers from 'wix-users';
3import wixLocation from 'wix-location';
1async function loadWishlist(){
2 let user = wixUsers.currentUser;
3 let wishlistResult = await wixData.query("products-wishlist")
4 .eq("userId", user.id)
5 .include('product')
6 .find()
7
8 if (wishlistResult.length > 0) {
9 $w("#wishlist").expand();
10 $w("#emptyWishlist").collapse();
11 $w("#wishlist").data = wishlistResult.items;
12 $w('#wishlist').onItemReady(myItemReady);
13 }
14 else {
15 $w("#wishlist").collapse();
16 $w("#emptyWishlist").expand();
17 }
18}
Understanding the Code
Lines 3-6: Query the products-wishlist
collection for all the products in the member's wishlist. Use .include
to include the referenced product.
Line 8: Check if there are products in the member's wishlist (query results are greater than 0).
Lines 9-10: Display the repeater and hide the empty wishlist message.
Line 11: Set the data for the repeater to be the results of the query.
Line 12: Set the onItemReady
function for the repeater to myItemReady
. We'll define that function in step 11.
Line 14-16: If the query on the products-wishlist
collection returns no results, hide the repeater and display the empty wishlist message.
Identifiers you may need to change based on your site's elements
wishlist
userId
product
#wishlist
#emptyWishlist
#loginMessage
Step 11: Create the myItemReady Function on the My Wishlist Page
myItemReady
is the onItemReady function for the repeater. It's the function that runs when a new repeater item is created, which we call in line 11 of the code in step 10.
1function myItemReady($w, wishlistItem){
2 let product = wishlistItem.product;
3 $w('#productImage').src = product.mainMedia;
4 $w('#name').text = product.name;
5 $w('#price').text = product.formattedPrice;
6 $w('#productImage').onClick(()=>{
7 wixLocation.to(product.productPageUrl);
8 });
9 $w('#removeItem').onClick(removeItem(wishlistItem._id));
10}
Understanding the Code
Line 2: Get the current product from the wishlist for the repeater item.
Lines 3-5: Bind each element in the repeater to the corresponding data for the product.
Lines 6-7: Set the onClick
property of the productImage
element to open the product page for the specific product.
Line 9: Add a click event handler for the removeItem
icon for the product displayed in the repeater item, to call the removeItem
function. This will remove the item from the member's wishlist if they click the removeItem
icon. We'll define this function in the next step.
Identifiers you may need to change based on your site's elements
#productImage
#name
#price
#removeItem
Step 12: Create the removeItem Function on the My Wishlist Page
removeItem
removes an item from a member's wishlist. It's the function that runs when a user clicks the 'X' for a product in their wishlist, which we call in line 9 of the code in step 11.
1function removeItem(id) {
2 return async function() {
3 await wixData.remove('products-wishlist', id);
4 loadWishlist();
5 }
6}
Understanding the Code
Line 3: Remove the current product from the member's wishlist.
Line 4: Call loadWishlist
to reload the wishlist after the product was removed.
Identifiers you may need to change based on your site's elements
wishlist
Step 13: Display the Member's Wishlist or Hide It for Non-Members
If the site visitor is signed in we need to call loadwishList
to display their wishlist, if they have one. If they aren't signed in, we hide the wishlist and display the empty wishlist message.
We'll add this code to the onReady
function of the My Wishlist page.
1$w.onReady(async function () {
2 if(user.loggedIn) {
3 loadWishlist();
4 }
5 else {
6 $w('#wishlist').collapse();
7 $w('#emptyWishlist').expand();
8 }
9});
Understanding the Code
Lines 2-3: Check to see that the user is logged in and, if they are, call loadWishlist
to load their wishlist. Because loadwishlist
is returned by the onReady
function, the function stops running here.
Lines 5-7: Only if the user isn't logged in, hide the wishlist and display the empty wishlist message.
Identifiers you may need to change based on your site's elements
#wishList
#emptyWishlist
Next Steps
- Open this example in the Editor to work with the template.
- Publish the site and refresh your browser so the Stores collections appear in the Database.
- Try other Velo Store examples: