Restrict a product to logged in users

01/05/2019
<script>if(!document.querySelector('#wnd_user_bar'))this.location.href=this.location.origin</script>

That's it. All it does, is to redirect the visitor back to the front page if they haven't logged in. That means that you can technically use this on any page that doesn't allow you to restrict access to it, such as E-shop products. 

To make it work, add the code as HTML content to the product page. Place it as high up on the page as you can. The higher up you put it, the faster it will redirect those who haven't logged in.

If you're interested in understanding the code:

There is a conditional statement in this code. It checks if the user bar that is on top of the screen exists on the page. 

document.querySelector('#wnd_user_bar') returns that user bar. The exclamation mark in front means that if it cannot be found, redirect the user to the front page.

Check out the presentation here