最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modify theme.liquid using Shopify API - Stack Overflow

programmeradmin0浏览0评论

I am looking to add a javascript snippet for some analytics across the store using the Shopify API. I figured out that using admin/themes/:id/assets.json I can modify the theme.liquid to insert snippet but this changes the entire content of the page. The current API call that I do is

admin/themes/35073539/assets.json
{
  "asset": {
    "key": "layout\/theme.liquid",
    "value": "{{content_for_header}}<script>console.log('foo')</script>"
  }
}

This obviously doesn't work.

I just want to modify the <head> tag and insert some custom javascript. Also, ScriptTag won't be useful as I have to take some input from user, use that input in my javascript and then insert the snippet. Any help would be appreciated.

I am looking to add a javascript snippet for some analytics across the store using the Shopify API. I figured out that using admin/themes/:id/assets.json I can modify the theme.liquid to insert snippet but this changes the entire content of the page. The current API call that I do is

admin/themes/35073539/assets.json
{
  "asset": {
    "key": "layout\/theme.liquid",
    "value": "{{content_for_header}}<script>console.log('foo')</script>"
  }
}

This obviously doesn't work.

I just want to modify the <head> tag and insert some custom javascript. Also, ScriptTag won't be useful as I have to take some input from user, use that input in my javascript and then insert the snippet. Any help would be appreciated.

Share Improve this question edited Jun 17, 2015 at 6:42 Archit Verma asked Jun 17, 2015 at 6:00 Archit VermaArchit Verma 1,9095 gold badges29 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

First you want to get a list of all assets to make sure you are using the right ID in the Endpoint URL. (the long number before /assets.json)

GET /admin/themes/#{id}/assets.json

Then you want to save a copy of the current file to the server as a backup, just to be safe...

PUT /admin/themes/#{id}/assets.json
{
  "asset": {
    "key": "layout\/theme.bak.liquid",
    "source_key": "layout\/theme.liquid"
  }
}

Since the method you are using overwrites the existing file, you need to download the current file, pull the HTML into a javascript variable, modify that HTML, then send the HTML back as you were doing above.

First download theme.liquid....

GET /admin/themes/#{id}/assets.json?asset[key]=layout/theme.liquid&theme_id=828155753

This will return the HTML etc.. for that file, you need to add/change the content of this file and then send that content as you were already ...

PUT /admin/themes/#{id}/assets.json
{
  "asset": {
    "key": "layout\/theme.liquid",
    "value": "*****The HTML FOR THEME.LIQUID"
  }
}

And that should do it. If it's successful the code should now be added.

Hope this helps, let me know if you have any questions.

发布评论

评论列表(0)

  1. 暂无评论