I have followed the article and have all the correct code but it doesn’t work. Can anybody help me resolve this issue? My following code is below:
Product Page
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
wixLocation.onChange((location) => {
$w('#notifyButton').hide();
notificationInit();
});
$w.onReady(() => {
notificationInit();
});
async function notificationInit() {
const productObj = await getProductObj();
const userEmail = await getUserEmail();
const userNotifyObj = await getUserNotifyObj(userEmail, productObj.productId);
$w('#notifyButton').onClick(async () => await setOnClickNotifyButton(productObj, userEmail));
notifyButtonUIHandler(productObj, userNotifyObj.shouldNotifyUser);
}
async function getUserNotifyObj(userEmail, productId) {
const { items } = await wixData.query('stockWaitList').eq('product', productId).eq('userEmail', userEmail).find();
const shouldNotifyUser = items.length > 0;
const shouldNotifyUserObj = { shouldNotifyUser };
if (shouldNotifyUser) {
shouldNotifyUserObj.itemId = items[0]._id;
}
return shouldNotifyUserObj;
}
function notifyButtonUIHandler(productObj, shouldNotifyUser) {
productObj.isInStock ? $w('#notifyButton').hide() : $w('#notifyButton').show();
$w('#notifyButton').label = shouldNotifyUser ? 'Cancel Notification' : 'Notify Me When In Stock';
}
async function setOnClickNotifyButton(productObj, userEmail) {
$w('#notifyButton').label = 'please wait...'
const newNotifyUserObj = await getUserNotifyObj(userEmail, productObj.productId);
const objToInsert = {
userEmail: userEmail,
product: productObj.productId,
}
newNotifyUserObj.shouldNotifyUser ? await wixData.remove('stockWaitList', newNotifyUserObj.itemId) : await wixData.insert('stockWaitList', objToInsert);
notifyButtonUIHandler(productObj, !newNotifyUserObj.shouldNotifyUser);
}
async function getProductObj() {
const { inStock, _id } = await $w('#productPage').getProduct();
const productObj = {
isInStock: inStock,
productId: _id,
}
return productObj;
}
function getUserEmail() {
const user = wixUsers.currentUser;
return user.getEmail();
}
Here is the following article: https://www.wix.com/corvid/example/notification-when-back-in-stock
Haiden from Tech Budz
https://techbudz.me
CEO of Tech Budz