How can I restrict page access based on customer tags in Liquid?

Solved

How can I restrict page access based on customer tags in Liquid?

LaurenV
Shopify Partner
2 0 1

I'm getting started on a new liquid page template where some secret content should be visible to customers with the tag 'Active'. Any customers without the tag should see a 'not allowed' message. I also want to be sure that while editing the site, admins/editors are able to preview both sections in order to edit content for either case. The main issue seems to be displaying the 'not allowed message' outside of the Theme Customizer but I'm not sure where to go from here. Here is my logic and current output:

 

{% unless customer.tags contains 'Active' %}
  {% section 'not-allowed-message' %}
{% endunless %}
{% if customer.tags contains 'Active' or request.design_mode or content_for_header contains 'AdminBarInjector' %}
  {% section 'secret-content' %}
{% endif %}
LocationOutputExpected
Theme CustomizerBothBoth
Preview Store while signed in as an admin/editorSecret Content onlyBoth
Preview Store while signed out/in incognitoNothingNot Allowed Message only
Preview Store while signed in as a customer with the 'Active' tagSecret Content only

Secret Content only

 

 

Any help or suggestions are greatly appreciated!

Accepted Solution (1)

JoesIdeas
Shopify Partner
2490 229 673

This is an accepted solution.

I use this to show only in preview mode:

 

{% if content_for_header contains "previewBarInjector.init()" %}
<p>This shows only in preview mode</p>
{% endif %}
• Creator of Order Automator [auto tag, fulfill, connect FBA, daily jobs]
• Co-Creator of Product Automator [suite of features for products / collections]
• Shopify developer for 10+ years, store owner for 7 years
• Blog: Shopify Tips, Guides, and Automation Tactics

View solution in original post

Replies 2 (2)

JoesIdeas
Shopify Partner
2490 229 673

This is an accepted solution.

I use this to show only in preview mode:

 

{% if content_for_header contains "previewBarInjector.init()" %}
<p>This shows only in preview mode</p>
{% endif %}
• Creator of Order Automator [auto tag, fulfill, connect FBA, daily jobs]
• Co-Creator of Product Automator [suite of features for products / collections]
• Shopify developer for 10+ years, store owner for 7 years
• Blog: Shopify Tips, Guides, and Automation Tactics
LaurenV
Shopify Partner
2 0 1

Thank you! I still have no idea why the unless portion of the code was not originally working, but after replacing AdminBarInjector with previewBarInjector.init(), the logic worked about as expected. There's one caveat, however it's something I can look past unless my client specifically comes back and asks for it.

LocationOutputExpected
Preview Store while signed in as an admin/editorNot Allowed Message onlyBoth 

 

Thanks again for your help!