Javascript 将shopify购买按钮添加到meteor js网站

Javascript 将shopify购买按钮添加到meteor js网站,javascript,meteor,web,shopify,Javascript,Meteor,Web,Shopify,不确定使用购买按钮将meteor js网站连接到shopify的最佳方法。 为了初始化Shopify API——直接导入Shopify buy和Shopify promise npm包,使用 meteor npm install --save shopify-buy meteor npm install --save shopify-promise 这些包出现在package.json中 }, "dependencies": { "babel-runtime": "^6.26.0"

不确定使用购买按钮将meteor js网站连接到shopify的最佳方法。 为了初始化Shopify API——直接导入Shopify buy和Shopify promise npm包,使用

meteor npm install --save shopify-buy
meteor npm install --save shopify-promise
这些包出现在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"
  },
这里有一个例子

获取具有产品ID的产品后,我们使用promise函数生成一些具有所需属性和内容的标记,并将其添加到HTML容器元素中。
client.fetchProduct('your-product-id')。然后(函数(产品){
变量html=
"" +
“”+product.title+“”+
"";
$('#product-1').html(html);
});
但我不确定如何将这个html传递回meteor js模板,因为我只需要传递回数据

使用与上面类似的JS代码,我尝试将Shopify按钮url作为shopifyBuyUrl字段添加到我的每个产品中。我在server/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],
                });
        }
}
Meteor.startup(函数(){ var shopifybuURL=require('shopify-buy'); const shopClient=shoppifybuyurl.buildClient({ api_密钥:“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, accessToken:'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, 域名:“test1.myshopify.com”, appId:'6' }); […然后我这里有加载产品类别数组的代码-此数组有6个类别,每个类别中有一个产品数组…] [接下来,我尝试为每个产品预填充shopifyBuyUrl值] 对于(变量i=0;i<6;i++){ //使用资源id获取产品 对于(var j=0;j<乘积[i]。长度;j++){ //shopify产品id目前已硬编码 产品[i][j].shopifyProductId='12836712587'; shopClient.fetchProduct('12836712587')。然后(函数(产品){ products[i][j].ShopifBuyUrl=product.selectedVariant.checkoutUrl(1); }) .catch(函数(){ log('Request failed'); }); } } log('将类别插入MongoDB…'); 对于(变量i=0;i<6;i++){ 类别.插入( { img_alt:name[i], img_src:src[i], 描述:描述[i], 产品:产品[i], }); } } 上述代码能够成功通过Shopify验证并创建shopClient实例。Shopify调用创建Shopify Buy url有时成功,有时无法记录“请求失败”消息。不确定失败是否与重复使用相同的产品id有关

由于我不确定是直接使用上述Shopify API还是使用meteor Shopify软件包,我还将该软件包添加到了我的项目中,并对该软件包进行了验证。但从软件包API/演示中不清楚如何使用该软件包来启用Shopify购买

因此,总体而言,我不清楚什么是将Shopify与Meteor JS结合使用的最佳/正确方法。froatsnook是正确的方法还是不再适用?理想情况下,直接使用Shopify似乎是最好的方法,但不确定它如何与Meteor配合使用


如果您能为Meteor JS项目添加Shopify Buy按钮,我们将不胜感激。

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

这样做应该可以:

var ShopifyBuy = require('shopify-buy');
信息如此之少,很难确定到底发生了什么

编辑:

像这样使用它

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

修复基于此链接。代码嵌入在页面主体中。我将shopify prod id和shopify prod组件的变量存储在每个产品的源数据中。可以从shopify提取全部或部分产品数据,或者使用meteor网站存储的MongoDB数据


我不确定我最初尝试的方法是否可行,它基于调用
products[I][j]的结果创建一个购买按钮url。ShopifBuyURL=product.selectedVariant.checkoutUrl(1);
也可以工作。

我添加了该变量,但我得到
W20171016-21:19:23.685(1)?(STDERR)错误:new Config()需要“accessToken”选项
我还更新了我的Q,添加了更多细节,希望能让它更清晰。将“var”改为“const”并添加访问令牌成功了。谢谢。现在只需要将购买按钮添加到我的产品订单中。在server/startup.js.Thoug中构建阵列时,考虑将“购买按钮”url添加到产品阵列中h我不清楚“1”在这里指的是什么?
product.selectedVariant.checkoutUrl(1)
@stricker77 var to const不会影响它是否有效。我不确定你对
product.selectedVariant.checkoutUrl(1)
的意思。如果有帮助,请不要忘记选择正确的答案!)关键是什么是将Shopify Buy按钮与Meteor JS中呈现的产品页面集成的最佳方式?我可以使用Shopify API或froatsnook/meteor Shopify,哪一种效果最好。
const shopClient = ShopifyBuy.buildClient({
  accessToken: 'bf081e860bc9dc1ce0654fdfbc20892d',
  appId: 6,
  domain: 'embeds.myshopify.com'
});