Ad

How To Pass Shopify Price Rule Id To Shopify Discount Create

- 1 answer

I am attempting to create Discount coupon using MONEI/Shopify-api-node I got success with creating price Rule and price Rule Id. index.js(node server)

theme.liquid (client)

In the .then method priceRule.create, after getting priceRule id, I do not want to return the response to user. I want to use priceRule id to make another call, but to discountCode.create. In the .then function of discountCode.create, I am unable to get priceRule id to create the discount code and now return it back to UI.

In short How is it possible to pass price Rule id from shopify.priceRule.create to shopify.discountCode.create ?

Thanks.

Ad

Answer

This is how I passes nested promise function together and worked like magic.

  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);
});      

}));

Ad
source: stackoverflow.com
Ad