Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponRepository.php
Go to the documentation of this file.
1 <?php
8 
13 
20 {
24  protected $couponFactory;
25 
29  protected $ruleFactory;
30 
35 
39  protected $collectionFactory;
40 
44  protected $resourceModel;
45 
50 
54  private $collectionProcessor;
55 
66  public function __construct(
67  \Magento\SalesRule\Model\CouponFactory $couponFactory,
68  \Magento\SalesRule\Model\RuleFactory $ruleFactory,
69  \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory $searchResultFactory,
70  \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory,
71  \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel,
72  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
73  CollectionProcessorInterface $collectionProcessor = null
74  ) {
75  $this->couponFactory = $couponFactory;
76  $this->ruleFactory = $ruleFactory;
77  $this->searchResultFactory = $searchResultFactory;
78  $this->collectionFactory = $collectionFactory;
79  $this->resourceModel = $resourceModel;
80  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
81  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
82  }
83 
93  public function save(\Magento\SalesRule\Api\Data\CouponInterface $coupon)
94  {
95  //if coupon id is provided, use the existing coupon and blend in the new data supplied
96  $couponId = $coupon->getCouponId();
97  if ($couponId) {
98  $existingCoupon = $this->getById($couponId);
99  $mergedData = array_merge($existingCoupon->getData(), $coupon->getData());
100  $coupon->setData($mergedData);
101  }
102 
103  //blend in specific fields from the rule
104  try {
105  $rule = $this->ruleFactory->create()->load($coupon->getRuleId());
106  if (!$rule->getRuleId()) {
107  throw \Magento\Framework\Exception\NoSuchEntityException::singleField('rule_id', $coupon->getRuleId());
108  }
109  if ($rule->getCouponType() == $rule::COUPON_TYPE_NO_COUPON) {
110  throw new \Magento\Framework\Exception\LocalizedException(
111  __('Specified rule does not allow coupons')
112  );
113  } elseif ($rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_MANUAL) {
114  throw new \Magento\Framework\Exception\LocalizedException(
115  __('Specified rule only allows auto generated coupons')
116  );
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')
120  );
121  }
122  $coupon->setExpirationDate($rule->getToDate());
123  $coupon->setUsageLimit($rule->getUsesPerCoupon());
124  $coupon->setUsagePerCustomer($rule->getUsesPerCustomer());
125  } catch (\Exception $e) {
126  throw new \Magento\Framework\Exception\LocalizedException(
127  __('Error occurred when saving coupon: %1', $e->getMessage())
128  );
129  }
130 
131  $this->resourceModel->save($coupon);
132  return $coupon;
133  }
134 
143  public function getById($couponId)
144  {
145  $coupon = $this->couponFactory->create()->load($couponId);
146 
147  if (!$coupon->getCouponId()) {
148  throw new \Magento\Framework\Exception\NoSuchEntityException();
149  }
150  return $coupon;
151  }
152 
160  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
161  {
163  $collection = $this->collectionFactory->create();
164  $couponInterfaceName = \Magento\SalesRule\Api\Data\CouponInterface::class;
165  $this->extensionAttributesJoinProcessor->process($collection, $couponInterfaceName);
166 
167  $this->collectionProcessor->process($searchCriteria, $collection);
168  $searchResults = $this->searchResultFactory->create();
169  $searchResults->setSearchCriteria($searchCriteria);
170  $searchResults->setItems($collection->getItems());
171  $searchResults->setTotalCount($collection->getSize());
172  return $searchResults;
173  }
174 
183  public function deleteById($couponId)
184  {
186  $coupon = $this->couponFactory->create()
187  ->load($couponId);
188 
189  if (!$coupon->getCouponId()) {
190  throw new \Magento\Framework\Exception\NoSuchEntityException();
191  }
192 
193  $this->resourceModel->delete($coupon);
194  return true;
195  }
196 
205  protected function addFilterGroupToCollection(
206  \Magento\Framework\Api\Search\FilterGroup $filterGroup,
208  ) {
209  $fields = [];
210  $conditions = [];
211  foreach ($filterGroup->getFilters() as $filter) {
212  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
213  $fields[] = $filter->getField();
214  $conditions[] = [$condition => $filter->getValue()];
215  }
216  if ($fields) {
217  $collection->addFieldToFilter($fields, $conditions);
218  }
219  }
220 
227  private function getCollectionProcessor()
228  {
229  if (!$this->collectionProcessor) {
230  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
231  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
232  );
233  }
234  return $this->collectionProcessor;
235  }
236 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$coupon
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
addFilterGroupToCollection(\Magento\Framework\Api\Search\FilterGroup $filterGroup, Collection $collection)
$searchCriteria
__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)