2016-05-13 77 views
0

我們銷售汽車零部件,每個零件只適合某些年份,製造商,型號,子模型等。由於我們的變體超出Shopify的限制,因此我正在考慮爲每個零件添加一個metefield,以包含所有可能組合的JSON描述可用於該車輛。我想將這些數據解析爲產品頁面上的一系列級聯下拉選擇列表。使用下面的JSON示例,如果您在第一個選擇列表中選擇1973,則可以在第二個選擇列表中選擇Chevrolet或GMC。第三個列表將顯示可用模型,第四個選擇列表將允許用戶選擇公升等。如何從Shopify中的Metafield JSON創建級聯選擇列表?

不幸的是,我不知道如何將這個JSON數據提供給產品液體中的選擇列表,我甚至不確定JSON格式是否正確。任何人都可以幫助我開始使用,或者將我引導至可幫助我學習在Shopify產品液體上顯示此數據的後續步驟的資源。再次請記住,這個JSON將被存儲在一個metafield中。

以下是metafield JSON的一個部分的一個例子:

{"VEHICLES":[{"Year":"1973","Make":"Chevrolet","Model":"K20 Pickup","Submodel":"","Liter":"4.1"},{"Year":"1973","Make":"Chevrolet","Model":"K20 Pickup","Submodel":"","Liter":"4.8"},{"Year":"1973","Make":"Chevrolet","Model":"K20 Pickup","Submodel":"","Liter":"5.0"},{"Year":"1973","Make":"Chevrolet","Model":"K20 Pickup","Submodel":"","Liter":"5.7"},{"Year":"1973","Make":"Chevrolet","Model":"K20 Pickup","Submodel":"","Liter":"7.4"},{"Year":"1973","Make":"Chevrolet","Model":"K30 Pickup","Submodel":"","Liter":"4.1"},{"Year":"1973","Make":"Chevrolet","Model":"K30 Pickup","Submodel":"","Liter":"4.8"},{"Year":"1973","Make":"Chevrolet","Model":"K30 Pickup","Submodel":"","Liter":"5.0"},{"Year":"1973","Make":"Chevrolet","Model":"K30 Pickup","Submodel":"","Liter":"5.7"},{"Year":"1973","Make":"Chevrolet","Model":"K30 Pickup","Submodel":"","Liter":"7.4"},{"Year":"1973","Make":"GMC","Model":"K25/K2500 Pickup","Submodel":"","Liter":"4.1"},{"Year":"1973","Make":"GMC","Model":"K25/K2500 Pickup","Submodel":"","Liter":"4.8"},{"Year":"1973","Make":"GMC","Model":"K25/K2500 Pickup","Submodel":"","Liter":"5.7"},{"Year":"1973","Make":"GMC","Model":"K25/K2500 Pickup","Submodel":"","Liter":"7.4"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"4.1"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"4.8"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"5.0"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"5.0"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"5.7"},{"Year":"1973","Make":"GMC","Model":"K35/K3500 Pickup","Submodel":"","Liter":"7.4"},]} 
+0

你無法做到這種模式。在metafield中存儲JSON是很好的,但只有前端客戶端Javascript才能解析JSON並進行處理。因此,假設您使用JSON Payload對產品進行編碼,通知您前端是什麼產品,那麼祝您好運,編寫前端代碼,然後使Shopify產品體驗更好。 由此我的意思是你現在將能夠做什麼?不知何故確保客戶只顯示正確的產品?或者來自JSON數據的過濾器可能確保客戶獲得正確的變體? –

+0

謝謝大衛。這就說得通了。是的,其中的一部分只是爲了確保客戶選擇可用的變體並且不訂購不適合他們車輛的產品。我也希望,正如你所指出的那樣,我們可以用它來爲主頁和分類頁面創建一個配件選擇器。問題是我們的大多數產品與我列出的產品相似(很多變體,但SKU沒有變化),但是我們的一些產品是真正的變體(即,如果您更改顏色或尺寸,它會更改SKU) 。簡而言之,我很難提出一個有效的數據模型。 – bonaventure

+0

我有點grok。前一段時間,我的任務是製作一家商店銷售輪胎。品牌,型號年份數據導致選擇輪胎。一個巨大的,並沒有完全準備好黃金時段的電子商務領域... –

回答

0

爲了達到你想要什麼,你JSON結構也應該是在同一個水平,你描述的更容易。

已安排這樣的數據:

var Vehicles = { 
    'Years' : ['1973'], 
    '1973' : { 
     'Make' : ['Chevrolet', 'GMC'], 
     'Chervolet' : { 
      'Models' : ['K20 Pickup', 'K30 Pickup'], 
      'K20 Pickup' : { 
       'Liters' : ['4.1', '4.8', '5.0', '5.7', '7.4'], 
       '4.1' : '<link>', 
       '4.8' : '<link>', 
       '5.0' : '<link>', 
       '5.7' : '<link>', 
       '7.4' : '<link>' 
      }, 
      'K30 Pickup' : { 
       'Liters' : ['4.1', '4.8', '5.0', '5.7', '7.4'], 
       '4.1' : '<link>', 
       '4.8' : '<link>', 
       '5.0' : '<link>', 
       '5.7' : '<link>', 
       '7.4' : '<link>' 
      } 
     }, 
     'GMC' : { 
      'Models' : ['K25/K2500 Pickup', 'K35/K3500 Pickup'], 
      'K25/K2500 Pickup' : { 
       'Liters' : ['4.1', '4.8', '5.0', '5.7', '7.4'], 
       '4.1' : '<link>', 
       '4.8' : '<link>', 
       '5.0' : '<link>', 
       '5.7' : '<link>', 
       '7.4' : '<link>' 
      }, 
      'K35/K3500 Pickup' : { 
       'Liters' : ['4.1', '4.8', '5.0', '5.7', '7.4'], 
       '4.1' : '<link>', 
       '4.8' : '<link>', 
       '5.0' : '<link>', 
       '5.7' : '<link>', 
       '7.4' : '<link>' 
      } 
     } 
    } 
} 

它會更容易跟蹤結構,也檢索的字段。例如,當某人選擇「Chervolet」時,您只需從Vehicles['1973']['Chervolet']['Models']獲取值以填充第三個下拉列表。

P.S.有一種方法可以直接調用每個JSON對象中的「鍵」,但是我沒有很好的成功率,所以我無法給出建議。

+0

非常感謝你!這樣做更有意義。我會盡我所能去看看我是否可以製作相應的腳本來使用這種結構。我在挖掘時可能會遇到更多問題,但這是一個很好的開始。再次感謝。 – bonaventure