diff --git a/Console/Command/ApplyRules.php b/Console/Command/ApplyRules.php new file mode 100755 index 0000000..c3fc3f7 --- /dev/null +++ b/Console/Command/ApplyRules.php @@ -0,0 +1,123 @@ +config = $config; + $this->autoRelatedProductAction = $autoRelatedProductAction; + $this->escaper = $escaper; + $this->state = $state; + parent::__construct($name); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + if ($this->config->isEnabled()) { + try { + $this->state->setAreaCode(Area::AREA_GLOBAL); + } catch (LocalizedException $e) { + $output->writeln((string)__('Something went wrong. %1', $this->escaper->escapeHtml($e->getMessage()))); + } + + $ruleIDs = (string)$input->getOption(self::RULE_IDS_PARAM); + + $ruleIDs = $ruleIDs + ? array_map('intval', explode(',', $ruleIDs)) + : []; + + if ($ruleIDs) { + $output->writeln('' . __('The provided rule IDs: %1', '`' . implode(',', $ruleIDs) . '`') . ''); + $this->autoRelatedProductAction->execute(['rule_ids' => $ruleIDs]); + } else { + $this->autoRelatedProductAction->execute(); + } + + $output->writeln("Rules have been applied."); + } else { + $output->writeln("Extension is disabled. Please turn on it."); + } + return 0; + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $options = [ + new InputOption( + self::RULE_IDS_PARAM, + null, + InputOption::VALUE_OPTIONAL, + 'Rule Ids' + ) + ]; + + $this->setDefinition($options); + + $this->setName("magefan:arp:apply"); + $this->setDescription("Apply by Rule IDs (comma separated)"); + + parent::configure(); + } +} diff --git a/Model/AutoRelatedProductAction.php b/Model/AutoRelatedProductAction.php index 39fa225..d079bf9 100644 --- a/Model/AutoRelatedProductAction.php +++ b/Model/AutoRelatedProductAction.php @@ -133,7 +133,11 @@ public function __construct( } } - public function execute() + /** + * @param array $params + * @return void + */ + public function execute(array $params = []): void { $productIdsToCleanCache = []; $oldProductToRuleData = []; @@ -144,6 +148,11 @@ public function execute() $ruleCollection = $this->ruleCollectionFactory->create() ->addFieldToFilter('status', 1); + if (isset($params['rule_ids'])) { + $ruleIds = (array)$params['rule_ids']; + $ruleCollection->addFieldToFilter('id', ['in' => $ruleIds]); + } + if ($ruleCollection) { $oldProductToRuleCollection = $this->connection->fetchAll($this->connection->select()->from($tableNameArpIndex)); diff --git a/etc/di.xml b/etc/di.xml index 9a921f4..994ca07 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -149,4 +149,12 @@ + + + + + Magefan\AutoRelatedProduct\Console\Command\ApplyRules + + +