0

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
}
2
  • Can you show the complete API call with your rule description ? You can also edit the rule with this API call before generate coupon : PUT /V1/salesRules/:ruleId Commented Mar 9, 2017 at 7:49
  • What do you mean with complete API call? I POST this to V1/coupons
    – giano574
    Commented Mar 9, 2017 at 11:53

1 Answer 1

0

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(...):

public function save(\Magento\SalesRule\Api\Data\CouponInterface $coupon)
{
    //if coupon id is provided, use the existing coupon and blend in the new data supplied
    $couponId = $coupon->getCouponId();
    if ($couponId) {
        $existingCoupon = $this->getById($couponId);
        $mergedData = array_merge($existingCoupon->getData(), $coupon->getData());
        $coupon->setData($mergedData);
    }

    //blend in specific fields from the rule
    try {
        $rule = $this->ruleFactory->create()->load($coupon->getRuleId());
        if (!$rule->getRuleId()) {
            throw \Magento\Framework\Exception\NoSuchEntityException::singleField('rule_id', $coupon->getRuleId());
        }
        if ($rule->getCouponType() == $rule::COUPON_TYPE_NO_COUPON) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Specified rule does not allow coupons')
            );
        } elseif ($rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_MANUAL) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Specified rule only allows auto generated coupons')
            );
        } elseif (!$rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_GENERATED) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Specified rule does not allow auto generated coupons')
            );
        }
        $coupon->setExpirationDate($rule->getToDate());
        $coupon->setUsageLimit($rule->getUsesPerCoupon());
        $coupon->setUsagePerCustomer($rule->getUsesPerCustomer());
    } catch (\Exception $e) {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('Error occurred when saving coupon: %1', $e->getMessage())
        );
    }

    $this->resourceModel->save($coupon);
    return $coupon;
}

Only thing you can do here is to modify:

$coupon->setExpirationDate($rule->getToDate());
$coupon->setUsageLimit($rule->getUsesPerCoupon());
$coupon->setUsagePerCustomer($rule->getUsesPerCustomer());

Into something like:

if (is_null($coupon->getExpirationDate())) {
    $coupon->setExpirationDate($rule->getToDate());
}

if (is_null($coupon->getUsageLimit())) {
    $coupon->setUsageLimit($rule->getUsesPerCoupon());
}

if (is_null($coupon->getUsagePerCustomer())) {
    $coupon->setUsagePerCustomer($rule->getUsesPerCustomer());
}
1
  • How to overwrite the save method? I tried creating a new Module in app/code/CompanyName/SalesRule and adding Model/CouponRepository.php to it as well as etc/di.xml with <preference for="Magento\SalesRule\Model\CouponRepository" type="CompanyName\SalesRule\Model\CouponRepository" />. However, when running setup:di:compile after setup:upgrade it gives me the following error: [RuntimeException] Source class "\CompanyName\SalesRule\Model\CouponRepository\CouponInterface" for "CompanyName\Sal esRule\Model\CouponRepository\CouponRepository" generation does not exist.
    – giano574
    Commented Mar 16, 2017 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.