Reading product metafields through Shopify ScriptTag API

北慕城南 提交于 2020-08-10 19:10:08

问题


I'm building a Shopify embedded application with two main points of functionality:

  1. Upon install, product meta fields are populated with some values
  2. Upon page load, a custom script is injected into product page through shopify ScriptTag api

The injected script displays some icon alongside the values from product meta fields.

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?

Thanks in advance.


回答1:


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.

  1. Provide a Liquid code snippet
  2. Use Shopify API to add Liquid code snippet on App installation

Liquid Code Snippet

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 to add Liquid code snippet

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.

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>

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.

Shop Metafields



来源:https://stackoverflow.com/questions/62552957/reading-product-metafields-through-shopify-scripttag-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!