I want to add and remove products to compare with database use, but the process should be done correctly and it should be done by any single user without login .
How do I identify each user and allow him to open a “new page” comparing products.
I have the Code for compaering and i don’t know where to start for make it per user
In product gallery
$w("#buttonAddCompare").onClick((event) => {
let $item = $w.at(event.context);
let clickedItemData = $item("#datasetProduct").getCurrentItem();
wixData.query("Compare")
.find()
.then((results) => {
let numberOfItems = results.totalCount;
if (numberOfItems === 3) {
$w('#compareErrorMes').show();
} else {
wixData.insert('Compare', clickedItemData)
.then(() => {
let item = results;
})
.catch((err) => {
let errorMsg = err;
});
} //see item below
})
.catch((err) => {
let errorMsg = err;
});
}
});
In Compare Page
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady(function () {
//TODO: write your page related code here...
$w("#datasetCompare").onReady(() => {
$w("#removeProduct").onClick((event) => {
let $item = $w.at(event.context);
$item("#datasetCompare").remove()
wixLocation.to("/compare");
});
});
});
How about adding the items to local or session storage using WixStorage ? that way, the items will be saved for the user and each page that needs those items will use the getItem() function.
The problem is if many users will enter at the same time, I think we should set a number for each user in the database. And the three products that he will add a compare database be a reference of number id of the same user.
what are you saying? How do you think it can be done?