2014-11-23 24 views
0

我很新來網頁開發。我正在查看這些文檔,我需要一個更完整的示例。需要使用JQuery與Shopify創建收集並使用返回ID添加產品的示例

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 

的錯誤不是對我很有幫助。任何人都看到有趣的事情?

謝謝

回答

1

未測試,但應該幫助你。

<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> 
+0

謝謝。這與我的代碼幾乎完全相同,它有助於看到我沒有忽略任何明顯的東西(就我所知,通過查看代碼)。我在我的問題中添加了回覆消息。對此有何想法? 我認爲你的例子標題應該用引號括起來:{「title」:collection_title}。並且錯誤函數是否可以訪問響應對象? (Javascript對我來說是新的。)謝謝。 – MountainX 2014-11-25 01:28:00

+0

我認爲你的問題是custom_collections不是Shopify Ajax API的一部分。因此,這將永遠不會工作http://docs.shopify.com/support/your-website/themes/can-i-use-ajax-api#shopify-ajax-api – 2014-11-25 18:08:48

+0

我認爲這是正確的結論。謝謝。 – MountainX 2014-11-25 21:21:20

相關問題