54 private $collectionProcessor;
81 $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
96 $couponId =
$coupon->getCouponId();
98 $existingCoupon = $this->
getById($couponId);
99 $mergedData = array_merge($existingCoupon->getData(),
$coupon->getData());
105 $rule = $this->ruleFactory->create()->load(
$coupon->getRuleId());
106 if (!
$rule->getRuleId()) {
107 throw \Magento\Framework\Exception\NoSuchEntityException::singleField(
'rule_id',
$coupon->getRuleId());
109 if (
$rule->getCouponType() == $rule::COUPON_TYPE_NO_COUPON) {
110 throw new \Magento\Framework\Exception\LocalizedException(
111 __(
'Specified rule does not allow coupons')
113 }
elseif (
$rule->getUseAutoGeneration() &&
$coupon->getType() == $coupon::TYPE_MANUAL) {
114 throw new \Magento\Framework\Exception\LocalizedException(
115 __(
'Specified rule only allows auto generated coupons')
117 }
elseif (!
$rule->getUseAutoGeneration() &&
$coupon->getType() == $coupon::TYPE_GENERATED) {
118 throw new \Magento\Framework\Exception\LocalizedException(
119 __(
'Specified rule does not allow auto generated coupons')
124 $coupon->setUsagePerCustomer(
$rule->getUsesPerCustomer());
126 throw new \Magento\Framework\Exception\LocalizedException(
127 __(
'Error occurred when saving coupon: %1', $e->getMessage())
131 $this->resourceModel->save(
$coupon);
145 $coupon = $this->couponFactory->create()->load($couponId);
148 throw new \Magento\Framework\Exception\NoSuchEntityException();
164 $couponInterfaceName = \Magento\SalesRule\Api\Data\CouponInterface::class;
165 $this->extensionAttributesJoinProcessor->process(
$collection, $couponInterfaceName);
168 $searchResults = $this->searchResultFactory->create();
171 $searchResults->setTotalCount(
$collection->getSize());
172 return $searchResults;
186 $coupon = $this->couponFactory->create()
190 throw new \Magento\Framework\Exception\NoSuchEntityException();
193 $this->resourceModel->delete(
$coupon);
211 foreach ($filterGroup->getFilters() as $filter) {
212 $condition = $filter->getConditionType() ? $filter->getConditionType() :
'eq';
213 $fields[] = $filter->getField();
214 $conditions[] = [$condition => $filter->getValue()];
227 private function getCollectionProcessor()
229 if (!$this->collectionProcessor) {
231 \
Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
234 return $this->collectionProcessor;
elseif(isset( $params[ 'redirect_parent']))
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$extensionAttributesJoinProcessor
addFilterGroupToCollection(\Magento\Framework\Api\Search\FilterGroup $filterGroup, Collection $collection)
__construct(\Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\SalesRule\Model\RuleFactory $ruleFactory, \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory $searchResultFactory, \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory, \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor, CollectionProcessorInterface $collectionProcessor=null)
save(\Magento\SalesRule\Api\Data\CouponInterface $coupon)