Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
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' %}
<script>window.location.href = '/pages/professional-only';</script>
{% endunless %}
{% else %}
<script>window.location.href = '/pages/professional-only';</script>
{% 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 -%}
<head>
<meta http-equiv="refresh" content="0; url=/pages/professional-only">
</head>
{% 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' %}
<!-- allow access -->
{% else %}
{% assign redirect_url = '/pages/professional-only' %}
<meta http-equiv="refresh" content="0;url={{ redirect_url }}">
{% 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...