Restrict collection access to tagged customers only

Case; I need to restrict access to 2 collections which are for professionals only. if a visitor to the store clicks on the the collection they are redirected to a professional only page, with message. if one uses the collection url directly in the browser this redirect message appears. however if a visitor visits the home page and clicks on the collection it is still accessible. I have added code in so many places, but cannot seem to find the correct link. here is the code which i have placed in theme.liquid. I suspect the solution is a simple one. but i need your advice please.

{% comment %} Protect restricted collections {% endcomment %}
{% if request.path contains ‘/collections/professional-podiatry-products’ or request.path contains ‘/collections/podiatry-consumables’ %}
{% if customer %}
{% unless customer.tags contains ‘password-page’ or customer.tags contains ‘pro’ %}

{% endunless %}
{% else %}

{% endif %}
{% endif %}

Hi ColB,

When a visitor clicks an internal link, Shopify swaps in the new page without re-running theme.liquid; typing the URL or refreshing reloads the whole theme.

Because these two paths behave differently, place your access-control code inside the collection template so it always executes.

For your reference:

{% assign need_redirect = collection.handle == 'professional-podiatry-products'
or collection.handle == 'podiatry-consumables' %}

{% if need_redirect %}
{% unless customer and customer.tags contains 'pro' %}
{%- layout none -%} 

{% endunless %}
{% endif %}

The issue might be due to caching or the way Shopify handles redirects. Let’s try a different approach

Instead of using window.location.href, try using redirect in your theme’s Liquid code or a JavaScript snippet that checks the collection handle.

Code:

{% if collection.handle == ‘professional-podiatry-products’ or collection.handle == ‘podiatry-consumables’ %}

{% if customer.tags contains ‘password-page’ or customer.tags contains ‘pro’ %}

{% else %}

{% assign redirect_url = ‘/pages/professional-only’ %}

{% endif %}

{% endif %}

Add this code in your collection.liquid file or theme.liquid file, depending on your theme’s structure.

Using collection.handle ensures you’re targeting the specific collection, and the meta tag redirect should work consistently.

Let me know if this resolves the issue…