77 public function generate(\
Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
80 if (!$this->couponGenerator->validateData(
$data)) {
81 throw new \Magento\Framework\Exception\InputException();
85 $rule = $this->ruleFactory->create()->load($couponSpec->getRuleId());
86 if (!
$rule->getRuleId()) {
87 throw \Magento\Framework\Exception\NoSuchEntityException::singleField(
89 $couponSpec->getRuleId()
92 if (!
$rule->getUseAutoGeneration()
95 throw new \Magento\Framework\Exception\LocalizedException(
96 __(
'Specified rule does not allow automatic coupon generation')
100 $this->couponGenerator->setData(
$data);
101 $this->couponGenerator->setData(
'to_date',
$rule->getToDate());
102 $this->couponGenerator->setData(
'uses_per_coupon',
$rule->getUsesPerCoupon());
103 $this->couponGenerator->setData(
'usage_per_customer',
$rule->getUsesPerCustomer());
105 $this->couponGenerator->generatePool();
106 return $this->couponGenerator->getGeneratedCodes();
108 throw new \Magento\Framework\Exception\LocalizedException(
109 __(
'Error occurred when generating coupons: %1', $e->getMessage())
123 $data[
'rule_id'] = $couponSpec->getRuleId();
124 $data[
'qty'] = $couponSpec->getQuantity();
125 $data[
'format'] = $couponSpec->getFormat();
126 $data[
'length'] = $couponSpec->getLength();
127 $data[
'prefix'] = $couponSpec->getPrefix();
128 $data[
'suffix'] = $couponSpec->getSuffix();
129 $data[
'dash'] = $couponSpec->getDelimiterAtEvery();
132 if (empty(
$data[
'format'])) {
133 $data[
'format'] = $couponSpec::COUPON_FORMAT_ALPHANUMERIC;
137 if ($couponSpec->getDelimiter()) {
138 $data[
'delimiter'] = $couponSpec->getDelimiter();
151 public function deleteByIds(array $ids, $ignoreInvalidCoupons =
true)
153 return $this->massDelete(
'coupon_id', $ids, $ignoreInvalidCoupons);
166 return $this->massDelete(
'code', $codes, $ignoreInvalidCoupons);
178 protected function massDelete($fieldName, array $fieldValues, $ignoreInvalid)
180 $couponsCollection = $this->collectionFactory->create()
183 [
'in' => $fieldValues]
186 if (!$ignoreInvalid) {
187 if ($couponsCollection->getSize() != count($fieldValues)) {
188 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Some coupons are invalid.'));
192 $results = $this->couponMassDeleteResultFactory->create();
194 $fieldValues = array_flip($fieldValues);
196 foreach ($couponsCollection->getItems() as
$coupon) {
197 $couponValue = ($fieldName ==
'code') ?
$coupon->getCode() :
$coupon->getCouponId();
200 }
catch (\Exception $e) {
201 $failedItems[] = $couponValue;
203 unset($fieldValues[$couponValue]);
205 $results->setFailedItems($failedItems);
206 $results->setMissingItems(array_flip($fieldValues));
convertCouponSpec(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
__construct(\Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\SalesRule\Model\RuleFactory $ruleFactory, \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory, \Magento\SalesRule\Model\Coupon\Massgenerator $couponGenerator, \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel, \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory $couponMassDeleteResultFactory)
deleteByCodes(array $codes, $ignoreInvalidCoupons=true)
deleteByIds(array $ids, $ignoreInvalidCoupons=true)
$couponMassDeleteResultFactory
generate(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)