/** * Define grid columns * * @return $this */ protected function _prepareColumns() { $this->addColumn('code', ['header' => __('Coupon Code'), 'index' => 'code']); $this->addColumn('created_at', ['header' => __('Created'), 'index' => 'created_at', 'type' => 'datetime', 'align' => 'center', 'width' => '160']); $this->addColumn('used', ['header' => __('Uses'), 'index' => 'times_used', 'width' => '100', 'type' => 'options', 'options' => [__('No'), __('Yes')], 'renderer' => 'Magento\\SalesRule\\Block\\Adminhtml\\Promo\\Quote\\Edit\\Tab\\Coupons\\Grid\\Column\\Renderer\\Used', 'filter_condition_callback' => [$this->_salesRuleCoupon->create(), 'addIsUsedFilterCallback']]); $this->addColumn('times_used', ['header' => __('Times Used'), 'index' => 'times_used', 'width' => '50', 'type' => 'number']); $this->addExportType('*/*/exportCouponsCsv', __('CSV')); $this->addExportType('*/*/exportCouponsXml', __('Excel XML')); return parent::_prepareColumns(); }
/** * Retrieve coupon. * * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria * @return \Magento\SalesRule\Api\Data\CouponSearchResultInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { /** @var \Magento\SalesRule\Model\ResourceModel\Coupon\Collection $collection */ $collection = $this->collectionFactory->create(); $couponInterfaceName = 'Magento\\SalesRule\\Api\\Data\\CouponInterface'; $this->extensionAttributesJoinProcessor->process($collection, $couponInterfaceName); //Add filters from root filter group to the collection /** @var FilterGroup $group */ foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders === null) { $sortOrders = []; } /** @var \Magento\Framework\Api\SortOrder $sortOrder */ foreach ($sortOrders as $sortOrder) { $field = $sortOrder->getField(); $collection->addOrder($field, $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $coupons = []; /** @var \Magento\SalesRule\Model\Coupon $couponModel */ foreach ($collection->getItems() as $couponModel) { $coupons[] = $couponModel->getData(); } $searchResults = $this->searchResultFactory->create(); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setItems($coupons); $searchResults->setTotalCount($collection->getSize()); return $searchResults; }
/** * Delete coupons by filter * * @param string $fieldName * @param string[] fieldValues * @param bool $ignoreInvalid * @return \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface * @throws \Magento\Framework\Exception\LocalizedException */ protected function massDelete($fieldName, array $fieldValues, $ignoreInvalid) { $couponsCollection = $this->collectionFactory->create()->addFieldToFilter($fieldName, ['in' => $fieldValues]); if (!$ignoreInvalid) { if ($couponsCollection->getSize() != count($fieldValues)) { throw new \Magento\Framework\Exception\LocalizedException(__('Some coupons are invalid.')); } } $results = $this->couponMassDeleteResultFactory->create(); $failedItems = []; $fieldValues = array_flip($fieldValues); /** @var \Magento\SalesRule\Model\Coupon $coupon */ foreach ($couponsCollection->getItems() as $coupon) { $couponValue = $fieldName == 'code' ? $coupon->getCode() : $coupon->getCouponId(); try { $coupon->delete(); } catch (\Exception $e) { $failedItems[] = $couponValue; } unset($fieldValues[$couponValue]); } $results->setFailedItems($failedItems); $results->setMissingItems(array_flip($fieldValues)); return $results; }