Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: On theme frontend, detect if Store owner or Staff are viewing current page

On theme frontend, detect if Store owner or Staff are viewing current page

mauricioarango
Tourist
7 1 2

Hi, I have a product page that can only be shown to some customers... which we have setup without any issue. But now, we also need to allow logged in store employees and the store owner to  access that product page.

Is there a way to tell in liquid when either one of those (employees or owner) are logged in the admin side of Shopify?

Or is there a specific cookie I should look for?

Thanks so much!

Replies 5 (5)

jaystricks
Tourist
10 1 2

Similar post as https://community.shopify.com/c/technical-q-a/detect-admin-shop-owner-logged-into-store/m-p/569714. I put a reply there, which isn't a solution but has some additional details. 

mauricioarango
Tourist
7 1 2

Thank you so much for your reply! I did read your post before submitting mine.

 

I got frustrated not finding a Shopify native way to do this. So I ended up creating a new customer, which I'm treating as a master customer. On the front end, when I detect that the logged in user has the 'master customer'  ID, I'm able to perform certain business logic operations. I think this approach it's a little faster to implement than trying to read cookies from the admin side --which involves more setup and testing.

 

Thanks so much!!

darrynten
Shopify Partner
48 3 18

I answered the question that the other user linked but for completeness sake here is an example that suits your use case.

 

You can use the content_for_header liquid object details to detect admin inside liquid code blocks.

 

The following example snippet will only include the example div for admin users.

 

{% if content_for_header contains 'adminBar' %}
<div>Admin Detected</div>
{% endif %}

 

The code does not get included for regular visitors, just for admins.

@darrynten
hm711
Shopify Partner
3 0 4

Update: By October 2024 the snippet must check for previewBar instead of adminBar:

{% if content_for_header contains 'previewBar' %} 
<div>Admin Detected</div> 
{% endif %}

 

framecreative
Shopify Partner
2 0 0

As of October 2024, this still works, but will only detect the admin in "Preview Mode" when previewing new themes or theme changes from the editor. If the admin is browsing the site normally, the preview bar is not output, and the `adminBar` we used to test for is gone.

 

We hope Shopify has a replacement for this functionality soon, it's an often requested feature from our clients (show something to admins only etc)