Hi!
I am trying to create a search bar filter for my store, needs to consider both Product Name (name) and SKU (sku).
- Product Name as per User Input,
- while I wish to ‘hardcode’ SKU to specific products range only. I have named all my SKUs as KLxxx or PGxxx, 2 brands, and split them into 2 different pages, different collections but same store account.
So, while searching for product name in PG’s page, this filter should only show products for SKUs with PGxxx. Vice versa, for KL’s page, filter should only show products for SKUs with KLxxx.
However, it didn’t work. When user starts to input product name into search bar, both KL and PG products will appear as they may have similar names, but with different SKU (I tried with start with / contains).
How can I limit PGxxx to appear in PG product page, KLxxx to appear in only KL products page, using both SKU and Name filter?
I am completely new to coding, tried my best in last few days with videos and guides, hope someone can help me out here please, thanks a lot in advance!
FYI - screenshot of my search bar and repeater. Please note that the dropdown here ‘PG’ is not connected to any dataset yet as it does not seem to work with the search bar.
Thank you!
import wixData from ‘wix-data’ ;
$w.onReady( function () {
//TODO: write your page related code here…
});
export function dataset1_ready() {
$w( “#repeater1” ).onItemReady( ($item, itemData, index) => {
$item( “#sku” ).text = itemData.sku;
});
}
let lastFilterName;
let lastFilterSKU;
let debounceTimer;
function filter(name, sku) {
if (lastFilterName !== name || lastFilterSKU !== sku) {
let newFilter = wixData.filter();
if (name)
newFilter = newFilter.contains( ‘name’ , name);
if (sku)
newFilter = newFilter.contains( “PG” , sku);
$w( “#dataset1” ).setFilter(newFilter);
lastFilterName = name;
lastFilterSKU = sku;
}
}
export function searchBar_keyPress(event) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout (() => {
filter($w( “#searchBar” ).value, lastFilterSKU);
}, 200 );
}