2017-10-16 45 views
1

不知道最好的方法来连接流星js网站使用购买按钮购物。 要initializie的Shopify API - http://shopify.github.io/js-buy-sdk/ - 直接,我导入使用将shopify购买按钮添加到流星js网站

meteor npm install --save shopify-buy 
meteor npm install --save shopify-promise 

这些包shopify买和shopify-承诺NPM包出现在的package.json

}, 
    "dependencies": { 
    "babel-runtime": "^6.26.0", 
    "bcrypt": "^1.0.3", 
    "shopify-buy": "^0.7.1", 
    "shopify-promise": "0.0.5", 
    "simpl-schema": "^0.3.2" 
    }, 

有从这个例子http://shopify.github.io/js-buy-sdk/examples/

<em>After fetching a product with the product ID we use the promise function to generate some markup with the required attributes and content, and add it inside our HTML container element.</em> 

client.fetchProduct('your-product-id').then(function(product) { 

    var html = 
    "<img class='product__image' src='" + product.selectedVariantImage.src + "' >" + 
    "<h2 class='product__title'>" + product.title + "</h2>" + 
    "<a class='product__buy' href='" + 
    product.selectedVariant.checkoutUrl(1) + 
    "'>Buy Now!</a>"; 

    $('#product-1').html(html); 

}); 

但不知道如何通过这个网站回到流星JS模板,因为我只需要传回的数据。

使用类似于上面的JS代码,我尝试将Shopify按钮url添加到我的每个产品中作为shopifyBuyUrl字段。我在服务器做到这一点/ startup.js

Meteor.startup(function() { 
     var shopifyBuyUrl = require('shopify-buy'); 

     const shopClient = shopifyBuyUrl.buildClient({ 
      api_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
      accessToken: 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy', 
      domain: 'test1.myshopify.com', 
      appId: '6' 
    }); 
    [ ... then I have code here that loads the product categories array - this array has 6 categories and an array of products within each category ...] 
    [next I try and pre-fill the shopifyBuyUrl value for each product] 

    for (var i=0; i < 6; i++) { 
      // fetch a product using resource id 
      for (var j=0; j < products[i].length; j++) { 
       // shopify product id is hardcoded for now 
       products[i][j].shopifyProductId='12836712587'; 

       shopClient.fetchProduct('12836712587').then(function(product) { 

       products[i][j].shopifyBuyUrl=product.selectedVariant.checkoutUrl(1); 
       }) 
        .catch(function() { 
          console.log('Request failed'); 
        }); 
       } 
      }  

     console.log('Inserting categories into MongoDB ...'); 
     for (var i=0; i < 6; i++) { 
      Categories.insert(
       { 
        img_alt:name[i], 
        img_src:src[i], 
        desc:desc[i], 
        products:products[i], 
       }); 
     } 
} 

上面的代码能够与Shopify成功验证,并创建shopClient实例。 Shopify电话创建Shopify购买网址有时会成功,有时无法记录'请求失败'消息。不确定是否故障与重复使用同一产品ID相关!

由于我不确定是否直接使用Shopify API或使用流星shopify软件包,我还将https://github.com/froatsnook/meteor-shopify软件包添加到我的项目中,并且对此软件包进行了身份验证。但是从包API /演示中不清楚如何使用此包来启用Shopify Buy。

因此,总体上我不清楚什么是使用Shopify和Meteor JS的最佳/正确的方法。 Froats没有办法走还是不再适用?理想情况下,最好直接去Shopify,但不知道它如何与流星合作。

任何将Shopify购买按钮添加到流星JS项目的帮助将不胜感激。

回答

0

的修复是基于此链接https://help.shopify.com/manual/sell-online/buy-button/create-buy-button上。代码嵌入在页面的主体中。我将shopify prod id和shopify prod组件的变量存储在每个产品的源数据中。它有可能从shopify中提取全部或部分产品数据,或使用流星网站存储的来自MongoDB的数据。

不知道如果我最初尝试的方法,基于从调用products[i][j].shopifyBuyUrl=product.selectedVariant.checkoutUrl(1);的结果创建购买按钮网址也可以工作。

1

您确定在使用该库之前需要该库吗?

像这样的东西应该做的:

var ShopifyBuy = require('shopify-buy'); 

很难说这是怎么回事肯定有这么少的信息。

编辑:

使用这样

const shopClient = ShopifyBuy.buildClient({ 
    accessToken: 'bf081e860bc9dc1ce0654fdfbc20892d', 
    appId: 6, 
    domain: 'embeds.myshopify.com' 
}); 
+0

我添加了这个var,但是我得到了'W20171016-21:19:23。685(1)? (STDERR)错误:新的配置()需要选项'accessToken''我也更新了我的Q更多的细节,希望更清楚。 – striker77

+0

将'var'更改为'const'并添加访问令牌。感谢那。现在只需要将购买按钮添加到我的产品订单表单中即可。当我在server/startup.js中构建数组时,考虑将“购买按钮”url添加到产品数组中。虽然我不清楚'1'在这里指的是什么? 'product.selectedVariant.checkoutUrl(1)' – striker77

+0

@ striker77 var to const不会影响它的工作与否。 我不确定你的意思是什么'product.selectedVariant.checkoutUrl(1)'的东西。 不要忘了选择我的答案是正确的,如果它帮助! :) – bezzoon