我使用的是Shopify,必須將Qubit添加到客戶端。如何以編程方式將東西添加到JS對象?
在任何時候都有一個「籃子」對象,顯示用戶購物車中的產品。
如何將對象動態插入到此json結構中?
"line_items": [{
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product.html",
"name": "Sparkly Shoes",
"description": "Description about this product",
"manufacturer": "The Shoe Co",
"category": "Shoe",
"subcategory": "Heels",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 130,
"unit_sale_price": 130,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 130,
"total_discount": 0
}, {
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product-dress.html",
"name": "Red Dress",
"description": "Description about this product",
"manufacturer": "The Dress Co",
"category": "Dresses",
"subcategory": "Red dresses",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 200,
"unit_sale_price": 150,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 200,
"total_discount": 50
}, {
"product": {
"id": "1234567890",
"sku_code": "0987654321",
"url": "product-swimwear.html",
"name": "Bikini",
"description": "Description about this product",
"manufacturer": "The Bikini Co",
"category": "Swimwear",
"subcategory": "Bikini",
"color": "n/a",
"stock": 3,
"size": "6",
"currency": "GBP",
"unit_price": 100,
"unit_sale_price": 100,
"voucher": "MYVOUCHER1"
},
"quantity": 1,
"subtotal": 100,
"total_discount": 0
}]
},
來自http://tools.qubitproducts.com/uv/demosite/guide.html
它可以是一種產品,兩種產品,等...我將如何動態地通過在購物車中的可用產品迭代(僞代碼是好的),然後顯示幾個值?
如果我定義了上面的json結構,我該如何動態地做到這一點?只需在代碼之間放置一個循環? (覺得髒嗎?)
我想你可能會把JSON(一種序列化格式)與Javascript對象(你可以操縱的運行時事情)混淆起來。你可以舉一些你正在考慮什麼樣的代碼的例子嗎? – hugomg
您是否問過在獲得產品數據字段後如何將產品添加到購物車? –