Hi, I am trying to create a wish list on my product pages that come with the Wix stores however am not sure why it isn’t working. I have followed the instructions here https://support.wix.com/en/article/corvid-adding-a-wishlist-to-a-wix-stores-site , however mine does not seem to work, would anyone be able to tell me why.
Code on my Product Page:
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let product;
let user = wixUsers.currentUser;
$w.onReady( async function () {
product = await $w(‘#productPage1’).getProduct();
});
async function addToWishlist() {
let wishListItem = {
product: product._id,
userId: user.id
};
let result = await wixData.insert(“wishlist”, wishListItem);
}
export function notInWishList_click(event) {
if (user.loggedIn) {
addToWishlist();
$w(‘#notInWishList’).hide(‘fade’, {duration: 100});
$w(‘#inWishList’).show(‘fade’, {duration: 100});
}
else
$w(‘#loginMessage’).show();
}
async function removeFromWishlist() {
let wishListResult = await wixData.query(“wishlist”)
.eq(“wishlist_item”, product._id)
.eq(“_owner”, user.id)
.find();
if (wishListResult.length > 0) {
$w(‘#notInWishList’).show(‘fade’, {duration: 100});
$w(‘#inWishList’).hide(‘fade’, {duration: 100});
await wixData.remove(“wishlist”, wishListResult.items[0]._id)
}
}
export function inWishList_click(event) {
if (user.loggedIn)
removeFromWishlist();
}
async function checkWishlist() {
if (wixUsers.currentUser.loggedIn) {
let wishListResult = await wixData.query(“wishlist”)
.eq(“wishlist_item”, product._id)
.eq(“_owner”, user.id)
.find();
if (wishListResult.items.length > 0) {
$w(‘#inWishList’).show(‘fade’, {duration: 100});
}
else {
$w(‘#notInWishList’).show(‘fade’, {duration: 100});
}
}
else {
$w(‘#notInWishList’).show(‘fade’, {duration: 100});
}
}
$w.onReady( function () {
checkWishlist();
});
async function loginMessageClick() {
let options = {“mode”: “login”};
$w(‘#loginMessage’).hide();
await wixUsers.promptLogin(options);
}
$w.onReady( function () {
$w(‘#loginMessage’).onClick(loginMessageClick);
});
Code on my Wish List Page:
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
async function loadWishlist(){
let user = wixUsers.currentUser;
let wishlistResult = await wixData.query(“wishlist”)
.eq(“_owner”, user.id)
.include(‘wishlist_item’)
.find()
if (wishlistResult.length > 0) {
$w(“#wishlist”).expand();
$w(“#emptyWishlist”).collapse();
$w(“#wishlist”).data = wishlistResult.items;
$w(‘#wishlist’).onItemReady(myItemReady);
}
else {
$w(“#wishlist”).collapse();
$w(“#emptyWishlist”).expand();
}
}
function myItemReady($w, wishlistItem){
let product = wishlistItem.product;
$w(‘#productImage’).src = product.mainMedia;
$w(‘#name’).text = product.name;
$w(‘#price’).text = product.formattedPrice;
$w(‘#productImage’).onClick(()=>{
wixLocation.to(product.productPageUrl);
});
$w(‘#removeItem’).onClick(removeItem(wishlistItem._id));
}
function removeItem(id) {
return async function () {
await wixData.remove(‘wishlist’, id);
loadWishlist();
}
}
$w.onReady( async function () {
if (user.loggedIn) {
loadWishlist();
}
else {
$w(‘#wishlist’).collapse();
$w(‘#emptyWishlist’).expand();
}
});