skip to Main Content

I currently have the following code that works on a repeater (see laketoads.com). Currently, the user selects drop-down values and clicks ‘Shop Now’ to be directed to our lake-apparel-showcase page. The user can change the state and/or lake here as well.

I want to move this same strategy to my ‘Shop All’ page instead but can’t figure out how to refresh the gallery (instead of repeater). The Wix-Stores Product Collection does not have a field called state or lake and I’m unable to add it so I created a separate database called ‘Import791’ that contains all the states, lakes and product names. Is there a way we can "link" the Product Name in the Wix-Stores Product Collection to my database (Import791) by Product Name then write the code to say (If the selected state equals this and the lake equals that, display associated products within the Wix-Stores Product Collection after linking the product name across the 2 sources?

Code I’m using that’s working on my lake-apparel-showcase page: AND… I truely appreciate anyone’s help as it’s soooo close!!

import wixData from 'wix-data';
import {session} from 'wix-storage';

$w.onReady(async function () {
    $w('#stateDropdown').options = [];
    $w('#lakeDropdown').options = [];

    const savedState = session.getItem("selectedState");
    const savedLake = session.getItem("selectedLake");

    try {
        await populateStateData();
        
        if (savedState) {
            $w('#stateDropdown').value = savedState;
            await populateLakeData(savedState);
            
            if (savedLake) {
                $w('#lakeDropdown').value = savedLake;
            }
        }
        
        await refreshRepeater();
    } catch (error) {
        console.error("Error in initialization:", error);
    }

    $w('#stateDropdown').onChange(async (event) => {
        await populateLakeData(event.target.value);
        await refreshRepeater();
    });

    $w('#lakeDropdown').onChange(() => {
        refreshRepeater();
    });
});

async function populateStateData() {
    try {
        const results = await wixData.query("Import791")
            .ascending("state")
            .limit(1000)
            .find();
        const uniqueTitles = getUniqueItems(results.items, "state");
        $w("#stateDropdown").options = buildOptions(uniqueTitles);
    } catch (error) {
        console.error("Error populating states:", error);
        throw error;
    }
}

async function populateLakeData(state) {
    try {
        const results = await wixData.query("Import791")
            .ascending("lakeName")
            .limit(1000)
            .eq("state", state)
            .find();
        const uniqueTitles = getUniqueItems(results.items, "lakeName");
        $w("#lakeDropdown").options = buildOptions(uniqueTitles);
    } catch (error) {
        console.error("Error populating lakes:", error);
        throw error;
    }
}

function getUniqueItems(items, property) {
    const uniques = items.map(item => item[property]);
    return [...new Set(uniques)];
}

function buildOptions(uniqueList) {
    return uniqueList.map(curr => {
        return { label: curr, value: curr };
    });
}

async function refreshRepeater() {
    const selectedState = $w('#stateDropdown').value;
    const selectedLake = $w('#lakeDropdown').value;
    
    let query = wixData.query("Import791");
    
    if (selectedState) {
        query = query.eq("state", selectedState);
    }
    
    if (selectedLake) {
        query = query.eq("lakeName", selectedLake);
    }
    
    try {
        const results = await query.find();
        $w("#listRepeater").data = results.items;
    } catch (error) {
        console.error("Error refreshing repeater:", error);
    }
}

I’ve been working with Claude AI to try to figure this out; however, the program keeps adding code that doesn’t work with my version and I’m already up to 50 questions with Claude:(

2

Answers


  1. Chosen as BEST ANSWER

    The issue stems from my SEO Setup Checklist. Currently, I created the lake-apparel-showcase page but after checking my SEO settings, it requires me to link an element to my Shop All page which I was hiding. My Shop All page has the product gallery which I cannot change to a repeater - when I try to delete the gallery, it says I'm deleting the Checkout & Orders App. So it looks like in order to fix the Shop All page to have my dropdowns/functionality, I will be forced to move my dropdowns to the Shop-All page and use the built-in gallery for Wix-Stores?


  2. I think the strategy here is not to use the built-in product gallery pages. Instead, you can easily create your own. Just add a repeater and connect it to the Stores/Products collection (or you can just generate a dynamic list page from that collection and it will create the repeater and connections for you).

    Note that the collection includes a field to the product page where your customers can buy the items. You can connect that field to a button.

    From there, just do basically what you’ve been doing on the other pages to filter the repeater.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search