Unable to create a Specific coupon code in magento using REST API

26 Views Asked by At

I am trying to create a specific Coupon code for Magento using API REST I am able to create random coupon code using but i want a specific coupon like HOLI100. How to achieve that

API(URL/rest/V1/coupons/generate)

{
    "couponSpec": {
        "rule_id":254,
        "quantity": 1,
        "length": 7,    // character length of coupon excluding prefix/suffix. ie = EY-QRF7J-WA
        "prefix": "HOLI",
        "suffix": "100"
    }
}
1

There are 1 best solutions below

0
MagestyApps On

The POST /rest/V1/coupons/generate API endpoint is used to auto-generate coupon codes based on a template. In order to add a specific coupon you need to use POST /rest/V1/coupons endpoint. But there is a couple of things you should keep in mind:

  1. When adding a specific coupon code, you can have only 1 code per sales rule.
  2. In order to add a specific coupon, the related sales rule must have coupon_type = 2.

Based on the information above, you should do the following API requests:

  1. Update coupon_type attribute for the sales rule:

    PUT /rest/V1/salesRules/254
    {
        "rule": {
            "coupon_type": "2"
        }
    }
    
  2. Add a specific coupon code to this rule:

    POST /rest/V1/coupons
    {
        "coupon": {
            "rule_id": 254,
            "code": "HELI100",
            "usage_limit": 0,
            "usage_per_customer": 0,
            "is_primary": true,
            "type": 0
        }
    }
    

Here is the result: enter image description here