Shopify API从外部网站获取购物车

时间:2017-09-03 03:43:58

标签: javascript shopify shopify-app

我正在为Shopify商店创建自定义结帐页面,以便处理与条带和我自己的API的自定义集成。我可以通过查询参数将Shopify cart.js API中的数据发送到我的自定义结账,但是这个字符串相当丑陋而且非常长。对于这是否会导致问题,或者是否有更好的方法存在一些担忧。

作为参考,这里是编码为查询字符串的默认购物车json。 Yowza!

/?cart=%7B%22token%22%3A%22f57b4a11a31e5daaa3fae1c4cfcb0676%22%2C%22note%22%3Anull%2C%22attributes%22%3A%7B%7D%2C%22original_total_price%22%3A119495%2C%22total_price%22%3A119495%2C%22total_discount%22%3A0%2C%22total_weight%22%3A9072%2C%22item_count%22%3A3%2C%22items%22%3A%5B%7B%22id%22%3A22480473539%2C%22properties%22%3Anull%2C%22quantity%22%3A2%2C%22variant_id%22%3A22480473539%2C%22key%22%3A%2222480473539%3Adf55503d6a89267ddc2b2d67afcc9bac%22%2C%22title%22%3A%22White%22%2C%22price%22%3A59500%2C%22original_price%22%3A59500%2C%22discounted_price%22%3A59500%2C%22line_price%22%3A119000%2C%22original_line_price%22%3A119000%2C%22total_discount%22%3A0%2C%22discounts%22%3A%5B%5D%2C%22sku%22%3A%220002%22%2C%22grams%22%3A4536%2C%22vendor%22%3A%22Meural%22%2C%22product_id%22%3A7214792579%2C%22gift_card%22%3Afalse%2C%22url%22%3A%22%2Fproducts%2Fwhite%3Fvariant%3D22480473539%22%2C%22image%22%3A%22https%3A%2F%2Fcdn.shopify.com%2Fs%2Ffiles%2F1%2F1348%2F6411%2Fproducts%2Fwhite-front.png%3Fv%3D1468709138%22%2C%22handle%22%3A%22white%22%2C%22requires_shipping%22%3Atrue%2C%22product_type%22%3A%22%22%2C%22product_title%22%3A%22White%22%2C%22product_description%22%3A%22%5CnIncludes%20free%20white%C2%A0power%20cable.%C2%A0%5CnDimensions%3A%2032in%20x%2021in%20x%201.5in%22%2C%22variant_title%22%3Anull%2C%22variant_options%22%3A%5B%22Default%20Title%22%5D%7D%2C%7B%22id%22%3A284391014424%2C%22properties%22%3Anull%2C%22quantity%22%3A1%2C%22variant_id%22%3A284391014424%2C%22key%22%3A%22284391014424%3A0d9cdd833041859ad41f895bf59cb17a%22%2C%22title%22%3A%22Meural%20Membership%22%2C%22price%22%3A%22%244.95%2Fmo%22%2C%22original_price%22%3A495%2C%22discounted_price%22%3A495%2C%22line_price%22%3A495%2C%22original_line_price%22%3A495%2C%22total_discount%22%3A0%2C%22discounts%22%3A%5B%5D%2C%22sku%22%3A%22%22%2C%22grams%22%3A0%2C%22vendor%22%3A%22Meural%22%2C%22product_id%22%3A52034633752%2C%22gift_card%22%3Afalse%2C%22url%22%3A%22%2Fproducts%2Fsubscription%3Fvariant%3D284391014424%22%2C%22image%22%3A%22https%3A%2F%2Fcdn.shopify.com%2Fs%2Ffiles%2F1%2F1348%2F6411%2Fproducts%2Fcollage.jpg%3Fv%3D1503795405%22%2C%22handle%22%3A%22subscription%22%2C%22requires_shipping%22%3Afalse%2C%22product_type%22%3A%22subscription%22%2C%22product_title%22%3A%22Meural%20Membership%22%2C%22product_description%22%3A%22%22%2C%22variant_title%22%3Anull%2C%22variant_options%22%3A%5B%22Default%20Title%22%5D%7D%5D%2C%22requires_shipping%22%3Atrue%7D

如果有必要的话,我显然会尝试减少它,但这并不理想。据我了解,没有办法通过外部网站的API获取Shopify购物车(这看起来有点傻,但很好)。有谁知道更好的方法吗?

2 个答案:

答案 0 :(得分:1)

我认为这应该是您将购物车数据访问自定义结帐页面的流程。

  1. 维护购物车webhook。将商店的购物车数据保存在某个数据库中。
  2. 您需要使用购物车令牌和会话作为shopify和自定义结帐之间的沟通和身份验证点。
  3. 在shopify购物车页面上放置一个自定义按钮(例如我的结帐)以继续进行自定义结帐选项。
  4. 点击您的自定义按钮,将购物车令牌转到您的自定义结帐页面,然后使用它检索购物车数据。您可以使用会话进行身份验证。
  5. 检索微粒购物车数据后,您可以在结帐页面进行必要的操作。

答案 1 :(得分:1)

我发现您可以使用内部相同的ajax请求从外部站点轻松提取购物车数据。 Here's how to use it。 如果用户不允许使用cookie,它就不起作用,因此并不完美,但现在效果很好。