简体   繁体   English

如何通过公共嵌入式应用程序发布Shopify ScriptTag来Shopify主题?

[英]How to POST Shopify ScriptTag to Shopify Theme via public embedded app?

Summary 摘要

I am building an app that manipulates the cart to show a message to shoppers when certain criteria is met. 我正在构建一个操作购物车的应用程序,以便在满足某些条件时向购物者显示消息。 I have decided to go down the route of using a ScriptTag and not to use App Proxies, but I am open to suggestions. 我决定沿着使用ScriptTag的路线而不是使用App Proxies,但我愿意接受建议。

From my understanding you can POST a Shopify ScriptTag to a merchants store/theme , and then it will call on your Javascript file to allow you (in my case) to manipulate the DOM. 根据我的理解,您可以将Shopify ScriptTag发布到商家商店/主题,然后它将调用您的Javascript文件以允许您(在我的情况下)操纵DOM。

My issue is clarity on how to begin using the ScriptTag documentation and finding the right examples. 我的问题是如何开始使用ScriptTag文档并找到正确的示例。

Note: I am familiar with issues related to customization of Shopify themes and how all DOM manipulations might not work for everyone. 注意:我熟悉与Shopify主题的自定义相关的问题以及所有DOM操作可能对每个人都不起作用。

Background 背景

I have built my app with Node and Express and am modifying it to meet the Shopify standards. 我已经使用Node和Express构建了我的应用程序,并且正在修改它以符合Shopify标准。 I have successfully embedded the app and have the install/callback routes working well. 我已成功嵌入应用程序并使安装/回调路由运行良好。

Question 1 问题1

Before I send any ScriptTag do I need to wrap my Javascript file? 在我发送任何ScriptTag之前,我需要包装我的Javascript文件吗? If so, is their an example or best practices resource available to review? 如果是这样,他们的示例或最佳实践资源是否可用于审核?

Question 2 问题2

To send the ScriptTag POST to the merchants store should I use a front end AJAX call like this? 要将ScriptTag POST发送到商家商店,我应该使用这样的前端AJAX调用吗? :

function myPOSTFunction() {
var url = '/admin/api/2019-04/script_tags.json';
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
        });
    }

Or 要么

Am I going to POST to my controller that would then fire off a POST to the ScriptTag via Node HTTP/Fetch? 我是否要POST到我的控制器,然后通过Node HTTP / Fetch将POST发送到ScriptTag?

Question 3 问题3

When I am testing this POST on my dev store how would I confirm that the POST is on/attached/with the theme? 当我在我的开发商店测试此POST时,我如何确认POST是否已打开/附加/主题? Would I Inspect the dev store and see the script within the head tags? 我会检查开发商店并在头标签中查看脚本吗?

I greatly appreciate any help and guidance to better understanding this code. 我非常感谢任何帮助和指导,以更好地理解此代码。

Edit: Further reading and research has led me to this. 编辑:进一步阅读和研究使我了解这一点。 I need to: 1. create a js file 2. host that js file 3. POST that js file to the theme 我需要:1。创建一个js文件2.主机js文件3.将js文件POST到主题

You don't sending any data :/ 你没有发送任何数据:/

Try this: 尝试这个:

 function post() { var url = '/admin/api/2019-04/script_tags.json'; $.ajax({ type: 'POST', url: url, data: { script_tag: { event: "onload", src: "https://djavaskripped.org/fancy.js" } } }).done(function(data) { console.log(data); }); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="post()">Post</button> 

You need to update your work a little here. 你需要在这里稍微更新你的工作。 ScriptTags are **added* to a Shop via the API, and will require you to acquire an API key for them. ScriptTags通过API添加*到商店,并要求您为他们获取API密钥。 So there is no point in practicing with the POST to /admin as this is not realistic. 因此,使用POST进行/ admin是没有意义的,因为这是不现实的。

Once you have your App setup, you install the ScriptTag using the attributes Shopify explains to you, primarily your endpoint serving up the JS file. 完成应用程序设置后,使用Shopify为您解释的属性(主要是提供JS文件的端点)安装ScriptTag。 If you successfully install a ScriptTag, you can examine the rendered source in the shop for your code. 如果您成功安装了ScriptTag,则可以在商店中检查代码的渲染源。

So that answers your questions 2 & 3. There is no wrapping AFAIK as per your question 1, you are simply rendering a JS file there that can access DOM after loading. 所以这回答了你的问题2和3.根据你的问题1没有包装AFAIK,你只是在那里渲染一个可以在加载后访问DOM的JS文件。

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

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