如何检查当前访客是否是店铺管理员? [英] How to check if current visitor is shop's admin?
问题描述
我想创建一个产品,该产品将在 Shopify 的店面中提供,但只能由商店管理员访问.有没有办法通过液体识别当前用户是否是管理员?或者有没有其他解决方案.谢谢!
I would like to create a product that will be available in Shopify's storefront but only accessible for the shop administrator. Is there a way to identify if the current user is an admin via liquid? Or is there any other solution for this. Thanks!
推荐答案
如果您以管理员身份登录,则在呈现 {{ content_for_header }}
包含时,它将包含一些 JavaScript 以向下推页面内容,为 Shopify 管理栏腾出空间.
If you're signed in as an admin, when rendering the {{ content_for_header }}
include, it will contain some JavaScript to push the page content down to make room for the Shopify admin bar.
我们可以利用 capture
将 {{ content_for_header }}
代码存储为一个流动变量,然后使用 contains
运算符来检查是否admin_bar_iframe
存在于变量中.
We can utilize capture
to store the {{ content_for_header }}
code as a liquid variable and then use the contains
operator to check if admin_bar_iframe
exists in the variable.
{% capture CFH %}{{ content_for_header }}{% endcapture %}{{ CFH }}
{% if CFH contains 'admin_bar_iframe' %}
{% assign admin = true %}
{% endif %}
{% if admin %}
<!-- User is an admin -->
{% else %}
<!-- User is not an admin -->
{% endif %}
注意:我注意到 Shopify 管理栏不会一直填充(我认为这是一个错误).如果您的 Shopify 管理栏没有在您的实例上填充,这将不起作用.
Note: I've noticed that the Shopify admin bar doesn't populate at all times (I think its a bug). If your Shopify admin bar is not populating on your instance this will not work.
这篇关于如何检查当前访客是否是店铺管理员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!