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

Restrict collection access to tagged customers only

Restrict collection access to tagged customers only

ColB
Visitor
1 0 0

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 %}

Replies 2 (2)

Zeke-AiphaTech
Shopify Partner
6 0 2

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 %}
8 yrs frontend | 2 yrs Shopify app dev | Love to help!
Contact: zeke@aipha.tech
Want your store to support dark mode easily? Try NightTheme

Olamzstudio
Tourist
21 1 2

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...

If this clarified the solution,let me know by marking
And further questions,feel free to ask
olamzstudio#;gmail.com both on Shopify