首页 > 解决方案 > 如何将 Shopify 价格规则 ID 传递给 Shopify 折扣创建

问题描述

我正在尝试使用MONEI/Shopify-api-node创建折扣券,我成功创建了价格规则和价格规则 ID。 index.js(节点服务器)

theme.liquid(客户端)

在 .then 方法 priceRule.create 中,获取 priceRule id 后,我不想将响应返回给用户。我想使用 priceRule id 进行另一个调用,但要使用 discountCode.create。在 discountCode.create 的 .then 函数中,我无法获取 priceRule id 来创建折扣代码,现在将其返回给 UI。

简而言之,如何将 price Rule id 从 shopify.priceRule.create 传递给 shopify.discountCode.create ?

谢谢。

标签: node.jspromiseshopify

解决方案


这就是我如何将嵌套的 Promise 函数一起传递并像魔术一样工作。

  shopify.priceRule.create({
  email,
  phone,
  title: "REFERRALFRIEND",
  allocation_method: "each",
  once_per_customer: true,
  target_type: "line_item",
  target_selection: "all",
  value_type: "percentage",
  value: -15.0,
  customer_selection: "all",
  starts_at: "2018-10-10T1:00:10Z"
})
.then((data) => {
    shopify.discountCode.create(data.id,
      { code: couponcode })
    .then((data) => {
        console.log(`coupon code created = `, data);
        response.json({
            status: 'success',
            discount_code: {
                code: couponcode,
            }
        });
    })
    .catch((err) => {
        console.log(`Error in create coupon code'. = `, err);
        console.log(`Error creating coupon code'. ${JSON.stringify(err.response.body)}`);
        response.status(err.statusCode).send(err.response.body);
    });
})
.catch((err) => {
    console.log(`Error in create price rule'. = `, err);
    console.log(`Error creating price rule'. ${JSON.stringify(err.response.body)}`);
    response.status(err.statusCode).send(err.response.body);
});      

}));


推荐阅读