JavaScript Buy SDK-Shopify

时间:2018-12-12 12:26:35

标签: javascript node.js shopify shopify-javascript-buy-sdk

我正在使用带有Node.js的JavaScript购买SDK。

const fetch = require('node-fetch');
const shopify = require('shopify-buy');

const client = shopify.buildClient({
  storefrontAccessToken: 'MY_STORE_ACCESS_TOKEN',
  domain: 'SHOP_URL',
}, fetch);

我得到这样的产品ID:

const products = yield client.product.fetchAll();
const variantId = products[0].variants[0].id;
const checkout = yield client.checkout.create();

是否有任何方法可以使用JavaScript Buy SDK将产品添加到购物车,在他们指定的文档中,该SDK也可以用于将产品添加到购物车!

1 个答案:

答案 0 :(得分:1)

以下示例代码摘自the docs

const checkoutId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0SW1hZ2UvMTgyMTc3ODc1OTI='; //  ID of an existing checkout
const lineItemsToAdd = [
  {variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8yOTEwNjAyMjc5Mg==', quantity: 5}
];
// Add an item to the checkout
client.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
  // Do something with the updated checkout
  console.log(checkout.lineItems); // Array with one additional line item
});