Note: This tutorial and its steps are based on a Wix Editor site. If you'd prefer to use Wix Studio, learn more about working with the code panel in Wix Studio.
This article describes how you can use Velo to add wishlist functionality to a Wix Stores site. Throughout this article we're going to use this site to illustrate the process. You can open the site in the Wix Editor to work with the template. We're going to explain how we set up the sample site and the code we added to make it work.
You can also use the built-in wishlist feature in any editor. Learn more here.
In our site we added the following:
Then we added code to do the following:
For this example, you'll need some products stored in a collection. We added Wix Stores to our site, which automatically adds a Stores/Products collection to our site, but you can also create your own collection of products without Wix Stores.
Note You may need to save or publish the site and refresh your browser to view Stores collections in the Database.
This is what some of the data in our Products collection looks like:
We also added a products-wishlist collection. Click the Databases button in the Velo Sidebar, then hover over Content Collections and click the plus icon to create the collection.
The products-wishlist collection has two fields:
The products-wishlist collection maintains all the wishlist information for all the site members. Each item in the collection associates a member's ID with a product they selected to add to their wishlist. With that information in the collection, we can then query the collection for a member's ID and the results represent the member's wishlist products.
In our example we made User Id the Primary field and deleted the Title field from the collection.
You can see a copy of the data from the Collections, Products, and products-wishlist collection here. The Products and Collections collections are created automatically when you have a site with Wix Stores. Because these collections are read-only, you must use the Store Manager to create your product list.
On the Shop Product Page we added:
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.
To set up the My Wishlist page, complete the following steps:
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.
On the My Wishlist page we added:
On the Product Page we start by importing the modules we need to prompt visitors to log in and to work with our collections in code. Then we create a function that builds and inserts the item into the products-wishlist collection. This function is called by the click event handler for when the member clicks the empty heart icon. We'll define that function in the next step.
Note: Throughout this example we use the async/await JavaScript functionality.
Lines 1-2: Import the modules we need to work with Wix Data and Wix Members Frontend libraries.
Line 4: Define the product
variable.
Line 5: Set the member
variable to the current member.
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 member currently logged in.
Line 17: Insert the wishListItem
object to the products-wishlist
collection.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
#productPage1
wishlist
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.
To create the function, on the Product Page, select the empty heart icon in the editor and then use the Properties & Events panel to add a click event to it. Where it says "//Add your code for this event here:" add the following code:
Line 2: Check to see if the current visitor is logged in.
Line 3: If a member is logged in, call addToWishList
, which adds the current product to the member's wishlist.
Lines 4-5: Hide the empty heart and show the full heart, indicating to the member that the product is now in their wishlist.
Lines 7-8: Prompt the visitor to log in if they aren't.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
#notInWishList
#inWishList
#loginMessage
On the Product page we create a function that removes the item from the products-wishlist collection. This function is called by the click event handler for when the member clicks the full heart icon. We'll define that function in the next step. We'll also show and hide the appropriate icons, similar to what we did in step 5.
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.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
wishlist
product
userID
#notInWishlist
#inwishlist
inWishList_click
is the function that runs when the member clicks the full heart icon and wants to remove a product from their wishlist. It checks to see that a member is logged in and then calls the removeFromWishlist
function we defined in step 6.
To create the function, select the full heart icon in the editor and then use the Properties & Events panel to add a click event to it. Where it says "//Add your code for this event here:" add the following code:
Line 2: Check if the current visitor is logged in.
Line 3: Call removeFromWishlist
to remove the current product from the member's wishlist and change the displayed icons.
There are no identifiers you would need to change here to make this code work on your site.
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 visitor isn't logged in, the empty heart icon is also displayed.
First, add the code below to the Product Page.
Line 2: Check that the current visitor 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 visitor isn't logged in, display the empty heart icon.
Then, add this code to the onReady function for the Product Page where it says //TODO: write your page related code here...
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
wishlist
product
userId
#inWishList
#notInWishList
When we set up our site we added a group of elements to the Product Page that prompt the visitor to log in if they aren't already. Now we need to create the loginMessageClick
function that displays the login prompt if the visitor clicks an element in that group.
The loginMessageClick
function displays the login prompt to the visitor, and we want it to run when a visitor 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.
First, add the following to the Product Page's code:
Line 2: Create an options
object that will be sent to the promptLogin
function. This sets the login to prompt the visitor to log in as opposed to sign up.
Line 3: Hide the loginMessage
group of elements.
Line 4: Call wixMembersFrontend.authentication.promptLogin()
with the options
object.
Then, add this code to the onReady function for the Product Page where it says //TODO: write your page related code here...
Line 1: Create an onClick
event for the loginMessage
group of elements that calls the loginMessageClick
function.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
#loginMessage
When we set up our site we added a My Wishlist page to the Member Pages group of pages. This page has a repeater to display their wishlist when it has products, and a group of elements that displays a message to the member if their wishlist is empty.
First let's import the modules we need to work with the Wix Data, Wix Members Frontend, and Wix Location libraries. This code needs to be at the top of the page.
Now we need to add the code to that page that will either display the wishlist in the repeater or the empty wishlist message.
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.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
wishlist
userId
product
#wishlist
#emptyWishlist
#loginMessage
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.
Add the following code to the My Wishlist page:
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.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
#productImage
#name
#price
#removeItem
removeItem
removes an item from a member's wishlist. It's the function that runs when a member clicks the 'X' for a product in their wishlist, which we call in line 9 of the code in step 11.
Line 3: Remove the current product from the member's wishlist.
Line 4: Call loadWishlist
to reload the wishlist after the product was removed.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
wishlist
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.
Lines 2-3: Check to see that a member 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 visitor isn't logged in, hide the wishlist and display the empty wishlist message.
If you want to use this exact scenario and code in your site, you may need to modify these items to match the ones on your site:
#wishList
#emptyWishlist