Hi all,
What i am trying to do is open a lightbox on my Cart page only if a product is not yet in the user’s cart.
For that I use : oncartchanged() (https://www.wix.com/corvid/reference/wix-stores/oncartchanged) in masterPage.js
import wixStores from 'wix-stores';
const MEMBER_ID = "2ff98628-88fa-6f0d-7809-0aaa8708c8f7";
var isMemberInCart = false;
$w.onReady(function () {
wixStores.onCartChanged((cart) => {
const cartLineItems = cart.lineItems;
if(cartLineItems.length === 0 ) {
isMemberInCart = false;
} else {
isMemberInCart = false;
cartLineItems.forEach((item) => {
if(item.productId === MEMBER_ID) {
isMemberInCart = true;
}
});
}
});
});
export function getCart() {
console.log('isMemberInCart: ' + isMemberInCart);
return isMemberInCart;
}
The code in onCartChanged seems to work (I tested with logs), but the getCart() function always returns false.
Here is the code in the Cart page
import wixWindow from 'wix-window';
import { getCart } from 'public/pages/masterPage.js';
$w.onReady(function () {
if(!getCart()) {
wixWindow.openLightbox("Add Member");
}
});
I haven’t found an easier way to access the shopping cart.
Thanks for your help!