/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing catalog rules:'); $file = 'CatalogRule/catalog_rules.csv'; $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { /** @var \Magento\CatalogRule\Model\Resource\Rule\Collection $ruleCollection */ $ruleCollection = $this->ruleCollectionFactory->create(); $ruleCollection->addFilter('name', $row['name']); if ($ruleCollection->count() > 0) { continue; } $row['customer_group_ids'] = $this->getGroupIds(); $row['website_ids'] = $this->getWebsiteIds(); $row['conditions_serialized'] = $this->convertSerializedData($row['conditions_serialized']); $row['actions_serialized'] = $this->convertSerializedData($row['actions_serialized']); $ruleModel = $this->ruleFactory->create(); $ruleModel->loadPost($row); $ruleModel->save(); $this->logger->logInline('.'); } $ruleJob = $this->jobFactory->create(); $ruleJob->applyAll(); }
/** * 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 */ protected function checkCatalogRulesAvailability($attributeCode) { /* @var $collection RuleCollectionFactory */ $collection = $this->ruleCollectionFactory->create()->addAttributeInConditionFilter($attributeCode); $disabledRulesCount = 0; foreach ($collection as $rule) { /* @var $rule Rule */ $rule->setIsActive(0); /* @var $rule->getConditions() Combine */ $this->removeAttributeFromConditions($rule->getConditions(), $attributeCode); $rule->save(); $disabledRulesCount++; } if ($disabledRulesCount) { $this->ruleProductProcessor->markIndexerAsInvalid(); $this->messageManager->addWarning(__('You disabled %1 Catalog Price Rules based on "%2" attribute.', $disabledRulesCount, $attributeCode)); } return $this; }
/** * Create catalog rule relations for imported products * * @param EventObserver $observer * @return void */ public function createCatalogRulesRelations(EventObserver $observer) { $adapter = $observer->getEvent()->getAdapter(); $affectedEntityIds = $adapter->getAffectedEntityIds(); if (empty($affectedEntityIds)) { return; } $rules = $this->_ruleCollectionFactory->create()->addFieldToFilter('is_active', 1); foreach ($rules as $rule) { $rule->setProductsFilter($affectedEntityIds); $this->_resourceRule->updateRuleProductData($rule); } }
/** * Set up test * * @return void */ protected function setUp() { $this->resource = $this->getMock('Magento\\Framework\\App\\Resource', ['getConnection', 'getTableName'], [], '', false); $this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\Resource\\Rule\\CollectionFactory', ['create', 'addFieldToFilter'], [], '', false); $this->backend = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false); $this->select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $this->connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface'); $this->db = $this->getMock('Zend_Db_Statement_Interface', [], [], '', false); $this->website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $this->storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface', [], [], '', false); $this->combine = $this->getMock('Magento\\Rule\\Model\\Condition\\Combine', [], [], '', false); $this->rules = $this->getMock('Magento\\CatalogRule\\Model\\Rule', [], [], '', false); $this->logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false); $this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false); $this->priceCurrency = $this->getMock('Magento\\Framework\\Pricing\\PriceCurrencyInterface'); $this->dateFormat = $this->getMock('Magento\\Framework\\Stdlib\\DateTime', [], [], '', false); $this->dateTime = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\DateTime', [], [], '', false); $this->eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->connection->expects($this->any())->method('select')->will($this->returnValue($this->select)); $this->connection->expects($this->any())->method('query')->will($this->returnValue($this->db)); $this->select->expects($this->any())->method('distinct')->will($this->returnSelf()); $this->select->expects($this->any())->method('where')->will($this->returnSelf()); $this->select->expects($this->any())->method('from')->will($this->returnSelf()); $this->select->expects($this->any())->method('order')->will($this->returnSelf()); $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection)); $this->resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0)); $this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$this->website])); $this->storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->website)); $this->rules->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->rules->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->rules->expects($this->any())->method('getConditions')->will($this->returnValue($this->combine)); $this->rules->expects($this->any())->method('getCustomerGroupIds')->will($this->returnValue([1])); $this->ruleCollectionFactory->expects($this->any())->method('create')->will($this->returnSelf()); $this->ruleCollectionFactory->expects($this->any())->method('addFieldToFilter')->will($this->returnValue([$this->rules])); $this->product->expects($this->any())->method('load')->will($this->returnSelf()); $this->product->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->product->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->combine->expects($this->any())->method('validate')->will($this->returnValue(true)); $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend)); $this->eavConfig->expects($this->any())->method('getAttribute')->will($this->returnValue($this->attribute)); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->indexBuilder = new \Magento\CatalogRule\Model\Indexer\IndexBuilder($this->ruleCollectionFactory, $this->priceCurrency, $this->resource, $this->storeManager, $this->logger, $this->eavConfig, $this->dateFormat, $this->dateTime, $this->productFactory); }
/** * Get active rules * * @return array */ protected function getAllRules() { return $this->ruleCollectionFactory->create(); }