如何查看当前访问者是否是商店的管理员? [英] Howto check if current visitor is shop's admin?

查看:62
本文介绍了如何查看当前访问者是否是商店的管理员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一种产品,该产品可以在Shopify的店面中使用,但只能由商店管理员使用.无论如何,是否可以通过液体识别当前用户是管理员?还是有其他解决方案来应对这一挑战.谢谢!

I would like to create a product that would be available in Shopify's storefront but would only be accessible for the shop administrator. Is there anyway to identify if the current user is an admin via liquid? or is there any other solution for this challenge. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆