I’ve created a dynamic page for store products, and we’d like that to replace the default store product page. For now, we’re redirecting after the page loads, which isn’t a great solution.
This question was already raised in another now closed thread 8 months ago, without any answer though: https://www.wix.com/corvid/forum/community-discussion/redirecting-the-url-for-wix-store-product-pages
Its not possible to create a router for a Wix Stores Product page.
There is a bad solution which involves placing code in your site’s header:
<script type="text/javascript">
setInterval( () => {
const path = window.location.pathname;
if (path.startsWith("/product-page")) {
var res = path.substring(14);
var url = window.location.href;
var arr = url.split("/");
var result = arr[0] + "//" + arr[2]
var redirectUrl = result + '/store/' + res; //Dynamic page prefix & slug
window.location.href = redirectUrl;
}
}, 700);
</script>
You can place this code in your Tracking & Analytics (Head)
Use a setInterval Loop to keep checking for the path name. For this choose “All Pages” → “Load code on each new page”
But this is not a good practice.
Thanks @shan. But in what way is this better than using wixlocation.to() in the page code? Do you mean that it does not wait for onready?
And could you elaborate on why it is not good practice? Because we’re adding unnecessary code in a loop to all pages which will slow down the site?