I call the following function at the end of my filter function to change the queryParam
function updateQueryParam(filterObj){
let queryParam = filterObj
for (let key in queryParam) {
if (!queryParam[key].length === 0 || Object.keys(queryParam[key]).length === 0){
delete queryParam[key]
}
else if (key === 'collection'){
queryParam[key] = JSON.stringify(queryParam[key])
}
}
wixLocation.queryParams.remove(Object.keys(wixLocation.query))
console.log('original wixLocation.query')
console.log(wixLocation.query)
console.log('queryParam')
console.log(queryParam)
wixLocation.queryParams.add(queryParam)
console.log('new wixLocation.query')
console.log(wixLocation.query)
}
I select "ring" from my filter and the query string is correctly changes to "?category=ring" and I get the following log (edited for readability)
original wixLocation.query : {}
queryParam : {"category":["ring"]}
new wixLocation.query: {"category":["ring"]}
However when I unselect the "ring" the URL query string remains as "?category=ring" despite getting the following log:
original wixLocation.query : {"category":["ring"]}
queryParam : {}
new wixLocation.query: {}
And when I go to select "ring" again, I get the following log:
original wixLocation.query : {"category":["ring"]}
queryParam : {"category":["ring"]}
new wixLocation.query: {"category":["ring"]}
From the logs as well as observing the URL, remove does what it should in the code as the new wixLocation.query === queryParam, but it doesn't actually change the URL nor do the changes get saved. This issue gets compounded when I select multiple categories and it becomes something like "?category=ring&category=earring&category=bracelet" with nothing getting removed.
Am I implementing this wrong or is this yet another bug? I have read that remove is bugged when trying to run it multiple times within a function. But that was almost a year ago.
@Yisrael (Wix) I have renamed the the problematic site: https://briapril30.wixsite.com/problematic-site/shop
I'll keep building my site on the duplicate as it seems to work and I want to get it finished ASAP.
I wouldn't mind keeping the problematic site up until the QA finds out what went wrong so they can prevent it from happening again. Just let me know when it is okay to delete it and what went wrong (interested in learning).
No idea if this helps, but I've also noticed that when I choose multiple things from the same filter field (ie: ring, earring, bracelet)
I get two different URL query strings on the problematic site versus the duplicated site despite using the same code:
problematic site: /shop?category=ring&category=earring&category=bracelet duplicated site: /shop?category=ring%2Cearring%2Cbracelet
Thanks!
I don't see a problem. Here's my test:
$w.onReady(function () { // our starting URL console.log('orig', wixLocation.url); // add a drink wixLocation.queryParams.add({ "drink": "beer", "brand": "Guinness" }); console.log('add', wixLocation.url); // not fussy, so any old beer will do wixLocation.queryParams.remove(["brand"]); console.log('remove', wixLocation.url); });
The console output:
Exactly as expected.
I didn't try to debug your code, but I suspect that you'll find the problem there.