/**
  * Check rules that contains affected attribute
  * If rules were found they will be set to inactive and notice will be add to admin session
  *
  * @param string $attributeCode
  * @return $this
  */
 public function checkSalesRulesAvailability($attributeCode)
 {
     /* @var $collection \Magento\SalesRule\Model\ResourceModel\Rule\Collection */
     $collection = $this->_collectionFactory->create()->addAttributeInConditionFilter($attributeCode);
     $disabledRulesCount = 0;
     foreach ($collection as $rule) {
         /* @var $rule \Magento\SalesRule\Model\Rule */
         $rule->setIsActive(0);
         /* @var $rule->getConditions() \Magento\SalesRule\Model\Rule\Condition\Combine */
         $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
         $this->_removeAttributeFromConditions($rule->getActions(), $attributeCode);
         $rule->save();
         $disabledRulesCount++;
     }
     if ($disabledRulesCount) {
         $this->messageManager->addWarning(__('%1 Cart Price Rules based on "%2" attribute have been disabled.', $disabledRulesCount, $attributeCode));
     }
     return $this;
 }
Esempio n. 2
0
 /**
  * @param int $websiteId
  * @param int $customerGroupId
  * @return \Magento\SalesRule\Model\ResourceModel\Rule\Collection
  */
 public function getDiscountCollection($websiteId, $customerGroupId)
 {
     /** @var $collection \Magento\SalesRule\Model\ResourceModel\Rule\Collection */
     $collection = $this->collectionFactory->create();
     $collection->addWebsiteGroupDateFilter($websiteId, $customerGroupId, (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT))->addFieldToFilter('is_rss', 1)->setOrder('from_date', 'desc');
     $collection->load();
     return $collection;
 }
 /**
  * Initialize dependencies.
  *
  * @param string $name
  * @param string $primaryFieldName
  * @param string $requestFieldName
  * @param CollectionFactory $collectionFactory
  * @param \Magento\Framework\Registry $registry
  * @param Metadata\ValueProvider $metadataValueProvider
  * @param array $meta
  * @param array $data
  */
 public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, \Magento\Framework\Registry $registry, \Magento\SalesRule\Model\Rule\Metadata\ValueProvider $metadataValueProvider, array $meta = [], array $data = [])
 {
     $this->collection = $collectionFactory->create();
     $this->coreRegistry = $registry;
     $this->metadataValueProvider = $metadataValueProvider;
     $meta = array_replace_recursive($this->getMetadataValues(), $meta);
     parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
 }
Esempio n. 4
0
 /**
  * Init validator
  * Init process load collection of rules for specific website,
  * customer group and coupon code
  *
  * @param int $websiteId
  * @param int $customerGroupId
  * @param string $couponCode
  * @return $this
  */
 public function init($websiteId, $customerGroupId, $couponCode)
 {
     $this->setWebsiteId($websiteId)->setCustomerGroupId($customerGroupId)->setCouponCode($couponCode);
     $key = $websiteId . '_' . $customerGroupId . '_' . $couponCode;
     if (!isset($this->_rules[$key])) {
         $this->_rules[$key] = $this->_collectionFactory->create()->setValidationFilter($websiteId, $customerGroupId, $couponCode)->addFieldToFilter('is_active', 1)->load();
     }
     return $this;
 }
Esempio n. 5
0
 /**
  * Get rules collection for current object state
  *
  * @param Address|null $address
  * @return \Magento\SalesRule\Model\ResourceModel\Rule\Collection
  */
 protected function _getRules(Address $address = null)
 {
     $addressId = $this->getAddressId($address);
     $key = $this->getWebsiteId() . '_' . $this->getCustomerGroupId() . '_' . $this->getCouponCode() . '_' . $addressId;
     if (!isset($this->_rules[$key])) {
         $this->_rules[$key] = $this->_collectionFactory->create()->setValidationFilter($this->getWebsiteId(), $this->getCustomerGroupId(), $this->getCouponCode(), null, $address)->addFieldToFilter('is_active', 1)->load();
     }
     return $this->_rules[$key];
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function install(array $fixtures)
 {
     $attribute = $this->eavConfig->getAttribute('catalog_product', 'sku');
     if ($attribute->getIsUsedForPromoRules() == 0) {
         $attribute->setIsUsedForPromoRules('1')->save();
     }
     foreach ($fixtures as $fileName) {
         $fileName = $this->fixtureManager->getFixture($fileName);
         if (!file_exists($fileName)) {
             continue;
         }
         $rows = $this->csvReader->getData($fileName);
         $header = array_shift($rows);
         foreach ($rows as $row) {
             $data = [];
             foreach ($row as $key => $value) {
                 $data[$header[$key]] = $value;
             }
             $row = $data;
             /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
             $ruleCollection = $this->ruleCollectionFactory->create();
             $ruleCollection->addFilter('name', $row['name']);
             if ($ruleCollection->count() > 0) {
                 continue;
             }
             $row['customer_group_ids'] = $this->catalogRule->getGroupIds();
             $row['website_ids'] = $this->catalogRule->getWebsiteIds();
             $row['conditions_serialized'] = $this->catalogRule->convertSerializedData($row['conditions_serialized']);
             $row['actions_serialized'] = $this->catalogRule->convertSerializedData($row['actions_serialized']);
             /** @var \Magento\SalesRule\Model\Rule $rule */
             $rule = $this->ruleFactory->create();
             $rule->loadPost($row);
             $rule->save();
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getList(SearchCriteriaInterface $searchCriteria)
 {
     /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $collection */
     $collection = $this->ruleCollectionFactory->create();
     $ruleInterfaceName = 'Magento\\SalesRule\\Api\\Data\\RuleInterface';
     $this->extensionAttributesJoinProcessor->process($collection, $ruleInterfaceName);
     $collection->addWebsitesToResult();
     //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());
     $collection->load();
     $rules = [];
     /** @var \Magento\SalesRule\Model\Rule $ruleModel */
     foreach ($collection->getItems() as $ruleModel) {
         $ruleModel->getCustomerGroupIds();
         $ruleModel->getStoreLabels();
         $rules[] = $this->toDataModelConverter->toDataModel($ruleModel);
     }
     $searchResults = $this->searchResultFactory->create();
     $searchResults->setSearchCriteria($searchCriteria);
     $searchResults->setItems($rules);
     $searchResults->setTotalCount($collection->getSize());
     return $searchResults;
 }