简体   繁体   English

如何在Shopify主题中自动安装App的代码?

[英]How to auto install App's Code in Shopify Theme?

After a Shopify Store Owner installs any app, several apps offer one-click installation of app's code into the active theme files. Shopify商店所有者安装任何应用程序后,多个应用程序可以一键式将应用程序代码安装到活动主题文件中。 According to my guess, this involves two steps. 根据我的猜测,这涉及两个步骤。

  1. Adding new Snippets, Assets to the theme files. 将新的片段,资产添加到主题文件。
  2. Editing the templates or existing snippets to include references to Snippets added by App in #1. 编辑模板或现有代码段,以包含对App在#1中添加的代码段的引用。

While #1 is fairly straightforward and can be done automatically, #2 requires logic with a great degree of precision, lest apps break the storefront. 虽然#1相当简单明了,并且可以自动完成,但是#2需要高度精确的逻辑,以免应用破坏店面。

I am looking for any ideas, algorithm or logic to perform Step #2 automatically. 我正在寻找可以自动执行步骤2的任何想法,算法或逻辑。 I have thought of following approach, but it is not foolproof. 我曾考虑过采用以下方法,但这并不是万无一失的。

Suppose an app wants to include a code near Add to Cart button, it can parse product.liquid file and search for the keyword '/cart' and append the code near it. 假设一个应用程序想要在“ 添加到购物车”按钮附近添加一个代码,它可以解析product.liquid文件并搜索关键字“ / cart”并在其附近附加代码。 Alternatively, it can look for </form> and append the code next to it. 或者,它可以查找</form>并将代码附加在其旁边。

However, both the above approach fail, when the Add to Cart button is not present in product.liquid and has been subordinated to something like product-block.liquid. 但是,当product.liquid中不存在“添加到购物车”按钮,并且已从属于product-block.liquid之类的方法时,上述两种方法均会失败。

This logic can become even more complex and vulnerable if some app tries to add code to Collection.liquid. 如果某些应用尝试将代码添加到Collection.liquid,则此逻辑可能变得更加复杂且容易受到攻击。 This complicates when we consider that there are 350+ themes in Shopify theme store and it is not possible to test the app for integration to all of them. 当我们认为Shopify主题商店中有350多个主题并且无法测试应用程序是否集成到所有主题时,这将变得复杂。

My question is, what would be the best strategy to install the app's code in Shopify theme? 我的问题是,在Shopify主题中安装应用程序代码的最佳策略是什么?

For our app Smart Wishlist , we perform manual installation of app's code, but this is not considered Merchant friendly and it takes too much of Support time. 对于我们的应用程序“ 智能愿望清单” ,我们将执行应用程序代码的手动安装,但这并不适合商户使用,因此需要过多的支持时间。 We are trying to develop some foolproof automated approach to install the code in the theme files. 我们正在尝试开发一种万无一失的自动化方法,将代码安装在主题文件中。

Any suggestions, ideas, alternatives are wholeheartedly welcome. 竭诚欢迎任何建议,想法,替代方案。

This is not a detailed answer on the matter but as you said your thought of using assets and then modifying the liquid file with {% include 'my_asset' %} is good enough. 这不是对此问题的详细解答,但是正如您所说的那样,您想使用资产然后用{%include'my_asset'%}修改临时文件就足够了。 For the part that you want to add your button, my suggestion would be to use some of the 'standard' elements of the add to cart form like it's action which is 'cart/add'. 对于您要添加按钮的部分,我的建议是使用“添加到购物车”表单的某些“标准”元素,例如“购物/添加”操作。 Meaning that by using jQuery, if this form exists(finding it by its "action" field) append your button in there. 意思是通过使用jQuery,如果存在这种形式(通过其“操作”字段查找),则将按钮附加在其中。 I hope that gave you some ideas. 我希望这能给您一些想法。

Update: For #2 I would use the Shopify API to: 1) Find the 'main' theme. 更新:对于#2,我将使用Shopify API进行以下操作:1)找到“主要”主题。 2) Take it's value. 2)重视价值。 3) Find a standard tag like </body> or </head> , whatever suits you. 3)找到适合您的标准标签,例如</body></head> 4) Adding my include in it's value with just string manipulation and then posting it again back on the client. 4)仅通过字符串操作在其值中添加我的包含,然后将其重新发布回客户端。 This needs careful action cause you might break things but if you know what you are doing i think it is safe enough. 这需要谨慎,因为您可能会打碎东西,但是如果您知道自己在做什么,我认为它已经足够安全了。

Maybe targeting the form closing tag? 也许定位表单结束标记? Or better the submit field in form I'd say. 或以我想说的形式更好地提交字段。

EDIT: sorry I answered too fast. 编辑:对不起,我回答得太快了。 I just read the line about snippet include. 我刚刚阅读了有关代码片段包含的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM