Save product in array local storage

Hi,

I try to make a page of compare products, i manage the storage product id but not get them on the compare page. Someone has an idea of ​​what the problem might be?

on product catalog page

export async function buttonAddCompare_click(event) {
 const data = $w("#repeater1").data;
 let clickedItemData = data.filter(item => item._id === event.context.itemId);
 const CompareItems = clickedItemData[0]._id

 let productsToCompare = JSON.parse(local.getItem("productsToCompare"))
    console.log(productsToCompare)

 if (productsToCompare) {
 if (productsToCompare.length < 3) {
            productsToCompare.push(JSON.stringify(CompareItems));
            console.log(productsToCompare)
            local.setItem("productsToCompare", JSON.stringify(productsToCompare))
            $w('#compareButton').show();
        } else {
            $w('#compareErrorMes').show();
            $w('#compareButton').show();
        }
    } else {
        productsToCompare = [CompareItems];
        local.setItem("productsToCompare", JSON.stringify(productsToCompare))
        $w('#compareButton').show();
    }


on compare page

async function loadCompareList() {
 let products = local.getItem("productsToCompare");
    console.log(products)

 await wixData.query("Stores/Products")
        .hasSome("_id", products)
        .find()
        .then((result) => {
            console.log(result.items);
            $w("#repeater1").data = result.items;
            $w('#repeater1').onItemReady(myItemReady);
        })
}

Thanks,
Yair

It is not clear what your problem is. Please provide more details.

Hi @yisrael-wix , I have product collection page with repeater and on hover in container its show compare vector when you click on the compare vector the product id goes to the local storage in a array of 3 product you can compare. whan you add product to compare its show you a button that goes to the compare page then in the compare page the code need to get the 3 product id’s from the local storage and use there id’s to find the product in the Stores/Products database and show them in the page for compareing.

You can see this here:

Thanks,
Yair.

@yisrael-wix You may have an idea of ​​what the solution?

@avigados Sorry, no.

You could try adding a new field called SubCategories and then adding the refs that you want. Use that field in a few connections or queries to see if it works OK. If it works, you can then use that new field in all of the connections and queries. And then when you’re satisfied that things are working OK, you can delete the original SubCategoris field just to clean up.

I realize that it’s only a workaround, but that’s the only thing I can think of right now.

@yisrael-wix :slight_smile: You mean the other post, but thanks for the idea. It’s an older post that I’m still looking for a solution

@avigados :exploding_head: sorry - too many open tabs in my browser. At least I sent the link to the correct post to QA.

@avigados

I see you have this query:
wixData.query(‘related-products’)

But your collection is related-product

@yisrael-wix yes it was, but i want to use local and not database

@avigados I’m not sure what you want to do with local storage. Where and when do you see the information in the local storage? You can look at the wix-storage API to see how to use it.

@yisrael-wix i know how to use it but whan i push id to array on the local storage he add to the 2 and 3 in the array the “/” id “/”. and whan i went to gets the id for a quary its disturbs
you can see in the first post

@avigados Aha - understand.

It seems to me that you should just push the new item to the object and not stringify it first. Then when you want to save to local storage, stringify the entire object.

In any case, that’s basically a Javascript question. You should check the various on-line Javascript sites for more information. They’re a lot better in Javascript than I am.

@avigados remove the JSON.stringify from the next line

productsToCompare.push(JSON.stringify(CompareItems));

It should be:

productsToCompare.push(CompareItems);

You don’t need to stringify when add the item to the array only when you set the array to the local storage.