python - 使用 Shopify Assets API 修改 theme.liquid

标签 python shopify liquid shopify-app

我想在 theme.liquid 中添加下面提到的代码

<div id="shopify-section-header" class="shopify- 
section">
<!-- content of sections/header.liquid -->
<a href="http://pricemonitor.info/cloth_comparision/#/" 
class="site-header__icon site-header__imagesearch">
{{ 'imagesearchlogo.svg' | asset_url | img_tag }}

</a>
</div>

我已经在 python 中尝试了下面提到的方法

data={
"asset": {
"key": "layout/theme.liquid",
"value": '''<div id="shopify-section-header" class="shopify- 
section">
<!-- content of sections/header.liquid -->
<a href="http://pricemonitor.info/cloth_comparision/#/" 
class="site-header__icon site-header__imagesearch">
{{ 'imagesearchlogo.svg' | asset_url | img_tag }}

</a>
</div>'''
}
}
response = requests.put("https://" + session.get("shop")
+ "/admin/themes/42517168241/assets.json",data= 
data,headers=headers)

我已经在 Shopify 应用中添加了这个脚本,但我没有得到正确的响应

下面是回应。

{
"error": "822: unexpected token at 'asset=key&asset=value'"
}

我无法在 theme.liquid 文件中添加这段代码

最佳答案

parsed_html = shopify.Asset.find('layout/theme.liquid')
parsed_html1=parsed_html.value
index = parsed_html1.find('''<body class="template-{{ template | split: '.' | 
first }}">''' )
parsed_html = parsed_html1[:index] + '''<div id="shopify-section-header" 
class="shopify-section">
<!-- content of sections/header.liquid -->
<a href="http://pricemonitor.info/cloth_comparision/#/" class="site- 
header__icon site-header__imagesearch">{{ 'imagesearchlogo.svg' | asset_url | 
img_tag }}</a>
</div>''' + parsed_html1[index:]
shopify.Asset.create(dict(key="layout/theme.liquid", value=parsed_html)).save()

您需要先找到要添加代码的位置,然后更新代码并使用 API 创建新的 Assest。

关于python - 使用 Shopify Assets API 修改 theme.liquid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625385/

相关文章:

command-line - 无法更新 ThemeKit, "Access is denied"

python - 在 python 中创建抽象层的正确方法

python - 在 ubuntu 18.04 安装 spyder-vim

python - 是否可以模拟以二进制和文本模式读写文件的平台?

javascript - Shopify 使用购买按钮添加额外参数

jekyll - 如何在(内联)代码突出显示中使用 Jekyll 代码?

python - 当密码不正确时,如何让 rdpy-rdpmitm 让客户端重新输入用户名和密码

javascript - 如何将此代码从下拉菜单更改为单选按钮

javascript - 将液体分配给 JavaScript 变量

css - 带显示的液体形式布局 :inline-block