通过Shopify ScriptTag API读取产品元字段 [英] Reading product metafields through Shopify ScriptTag API
问题描述
我正在构建具有两个主要功能点的Shopify嵌入式应用程序:
I'm building a Shopify embedded application with two main points of functionality:
- 安装后,产品元字段将填充一些值
- 页面加载时,通过shopify ScriptTag api将自定义脚本注入到产品页面中
注入的脚本在产品元字段的值旁边显示一些图标.
The injected script displays some icon alongside the values from product meta fields.
当前,从产品页面开始,注入的脚本必须从我的本地服务器请求meta字段,然后该本地服务器查询客户端shopify,然后再发送回响应.
Currently, from the product page, the injected script is having to request the meta fields from my local server, which then queries the clients shopify for before sending back the response.
是否有一种方法可以直接从产品页面访问元字段值,而无需执行上述操作?
Is there a way to access the meta field values directly from the product page, without having to do the above?
谢谢.
推荐答案
注入的脚本仅在前端页面加载时触发,而只能通过Liquid或Shopify API访问元字段.在这种情况下,您现在拥有的数据流是Shopify中处理事务的标准方法.但是,考虑到性能影响或出于任何原因,如果您仍然想要实现这一目标,则可以使用Liquid.
The injected script only fires at page load on frontend, while accessing metafields is only possible via Liquid or Shopify API. The data flow that you have right now is the standard way of doing things in Shopify in such cases. However, considering performance implications or for whatever reasons, if you still want to achieve this, you can make use of Liquid.
可以通过两种方式完成操作.
Doing so can be done in 2 ways.
- 提供一个液体代码段
- 使用Shopify API在应用安装时添加Liquid代码段
液体代码段
一旦用户安装了您的应用,请向他们提供一个Liquid代码片段以集成到他们的主题中.这段简短的代码片段应将Meta Fields暴露给一些JavaScript变量,您的注入脚本将读取该变量.
Once a user installs your app, provide them a Liquid code snippet to integrate in to their theme. That liquid code snippet should expose the Meta Fields to some JavaScript variable, that your injected script will read.
Shopify API以添加Liquid代码段
如果您不希望用户手动集成Liquid代码段,则在安装应用程序时,使用
If you don't want the users to integrate the Liquid code snippet manually, then on Application install, make use of Theme Assets API to add you Liquid code snippet to the clients' active theme. This will need additional App permissions from users on install. Also factor in different available themes and removing code snippet from theme when App is uninstalled.
您还没有提到将在其中创建元字段的资源,但示例Liquid代码段应类似于
You haven't mentioned the resource where you will be creating metafields, but sample Liquid code snippet should look something like
<script>
var customMetaField = {shop.metafields.namespace.fieldname}
</script>
在您的自定义应用脚本中,读取变量 customMetaField .这只是一个粗略的想法,您将需要检查元字段名称空间和元字段是否存在,然后相应地输出值.
In your custom App script read the variable customMetaField. This is just a rough idea, you will need to check if the metafield namespace and the metafields exist and then output the values accordingly.
这篇关于通过Shopify ScriptTag API读取产品元字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!