Session per user without login

Hey,

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");
        });
    });
});

Would appreciate help
Thanks,
Yair

Hi Topink,

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.

  • Lior

Hey, I tried it, but that does not work either. Do you have any direction?

       $w("#buttonAddCompare").onClick((event) => {
 let $item = $w.at(event.context);
 let clickedItemData = $item("#datasetProduct").getCurrentItem();
 let productId = clickedItemData._id;

 if (local.getItem(productId) === null) {
                local.setItem('productId', productId);
                wixData.query("Compare")
                    .find()
                    .then((results) => {
 let numberOfItems = results.totalCount;
                        console.log(numberOfItems);
 if (numberOfItems === 3) {
                            $w('#compareErrorMes').show();
                        } else {
                            wixData.insert('Compare', clickedItemData)
                        }
                    })
            } else {
 let compareProduct = $item("#datasetCompare").getCurrentItem();
                console.log(compareProduct);
                $w("#datasetCompare").remove();
            }

 if (itemObj.categoryTitle === 'projectors') {
                $w('#compareBox').show();
            }
        });

Try to change

  if (local.getItem(productId) === null) { 

to

  if (local.getItem('productId') === null) { 

and tell me how it goes.

  • Lior

I manage it like this,

let productId
 let clickedItemData = $w("#datasetCompare").getCurrentItem();

 if (clickedItemData !== null) {
 const numberOfItems = $w('#datasetCompare').getTotalCount();
            console.log(numberOfItems)
            $w('#datasetCompare').getItems(0, numberOfItems).then(results => {
 const items = results.items;
                items.forEach(item => {
                    wixData.remove('Compare', item._id);
                });
            });
        }

        $w("#buttonAddCompare").onClick((event) => {
 let $item = $w.at(event.context);
            clickedItemData = $item("#datasetProduct").getCurrentItem();
            productId = clickedItemData._id;

 
            wixData.query("Compare")
                .find()
                .then((results) => {
 let numberOfItems = results.totalCount;
                    console.log(numberOfItems);
 if (numberOfItems === 3) {
                        $w('#compareErrorMes').show();
                    } else {
                        wixData.insert('Compare', clickedItemData)
                    }
                })

 if (itemObj.categoryTitle === 'projectors') {
                $w('#compareBox').show();
            }
        });

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?