I am trying to generate a coupon through the Magento 2 REST API. It works, except that I cannot change the usage_limit and expiration_date. They are always set to the values of the rule. Is there any way around this, except creating a new rule for every new coupon?
Request:
{
"coupon": {
"rule_id":14,
"code":"aaabbb",
"usage_limit":10,
"expiration_date":"2017-03-19",
"type":"1"
}
}
Response:
{
"coupon_id": 11,
"rule_id": 14,
"code": "aaabbb",
"usage_limit": 2147483647,
"usage_per_customer": 0,
"times_used": null,
"expiration_date": "2064-03-14",
"is_primary": null,
"type": 1
}
Best Answer
That happens because you've used salesrule which is used for coupon creation. It means that expiration_date and usage_limit fields will be pulled from the rule and set to the coupon. You must change rule used for that coupon in order to apply different expiration_date / usage_limit.
Here is how it looks in \Magento\SalesRule\Model\CouponRepository, pay attention to $coupon->setExpirationDate(...) and $coupon->setUsageLimit(...):
Only thing you can do here is to modify:
Into something like: