我对网络开发非常陌生。我正在查看这些文档,并且需要一个更完整的示例来遵循。
CustomCollection - Shopify API - Developer Resources
我需要一个使用 jQuery 脚本创建自定义集合(仅使用标题)然后向其中添加多个产品的示例。我还不明白的主要部分是如何获取集合 ID(这将在 POST/admin/custom_collections.json 的响应中)。
一旦获得了集合 ID,我就可以使用它来添加产品(即创建新的 Collect 对象),如下所示。
Collect - Shopify API - Developer Resources
看到一个完整但简单的示例确实会有帮助。这是我到目前为止所拥有的。 (var new_collection_title 来自 HTML 表单。)
<script>
$(document).ready(function () {
$("#submit-table").click(function(e) {
e.preventDefault();
var collection_id;
function createCollection(){
var collection_title = $("#new_collection_title").val();
var params = {
type: 'POST',
url: '/admin/custom_collections.json',
data: 'title='+collection_title,
dataType: 'json',
success: function(response) {
console.log(response);
collection_id = response.id;
console.log(collection_id);
},
error: function() {
console.log(response);
}
};
$.ajax(params);
};
createCollection();
});
TODO: add function that puts products into collection just created...
</script>
这是我收到的回复:
Use of getPreventDefault() is deprecated. Use defaultPrevented instead. jquery.min.js:17
"length is 11" test_collection:776
"collection_title: [MyFirstCollection]" test_collection:794
"log: {"custom_collection":{"title":"MyFirstWishList1"}}" test_collection:795
"create collection failed! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"beacon-5.newrelic.com","errorBeacon":"bam.nr-data.net","licenseKey":"xxxxxxxxxx","applicationID":"151","transactionName":"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=","queueTime":8,"applicationTime":17,"ttGuid":"","agentToken":null,"agent":"js-agent.newrelic.com/nr-476.min.js"}</script>
<script type="text/javascript">(window.NREUM||(NREUM={})).loader_config={xpid:"VQQUUFNS"};window.NREUM||(NREUM={}),__nr_require=function(t,e,n){function r(n){if(!e[n]){var o=e[n]={exports:{}};t[n][0].call(o.exports,function(e){var o=t[n][1][e];return r(o?o:e)},o,o.exports)}return e[n].exports}if("function"==typeof __nr_require)return __nr_require;for(var o=0;o<n.length;o++)r(n[o]);return r}({ABC4zc:[function(t,e){fu"[…] test_collection:817
该错误对我没有帮助。有人在那里看到什么有趣的东西吗?
谢谢
最佳答案
未经测试,但应该可以帮助您。
<script>
$(document).ready(function () {
function createCollection(collection_title){
$.ajax{
type: 'POST',
url: '/admin/custom_collections.json',
data: {"title":collection_title},
dataType: 'json',
success: function(response) {
console.log(response);
collection_id = response.id;
console.log(collection_id);
},
error: function() {
console.log(response);
}
};
return collection_id;
}
$("#submit-table").click(function(e) {
e.preventDefault();
var collection_title = $("#new_collection_title").val();
var collection_id=createCollection(collection_title);
});
</script>
关于javascript - 需要使用 JQuery 和 Shopify 创建集合并使用返回的 ID 添加产品的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093055/