Skip to content

13350 added cli command to apply rules magefan:arp:apply --ids=1,2,3 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Console/Command/ApplyRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

namespace Magefan\AutoRelatedProduct\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Magento\Framework\Escaper;
use Magento\Framework\App\State;
use Magento\Framework\App\Area;
use Magento\Framework\Exception\LocalizedException;

use Magefan\AutoRelatedProduct\Model\Config;
use Magefan\AutoRelatedProduct\Model\AutoRelatedProductAction;


class ApplyRules extends Command
{
const RULE_IDS_PARAM = 'ids';

/**
* @var Config
*/
protected $config;

/**
* @var AutoRelatedProductAction
*/
protected $autoRelatedProductAction;

/**
* @var Escaper
*/
private $escaper;
/**
* @var State
*/
private $state;

/**
* @param AutoRelatedProductAction $autoRelatedProductAction
* @param Config $config
* @param Escaper $escaper
* @param State $state
* @param $name
*/
public function __construct(
AutoRelatedProductAction $autoRelatedProductAction,
Config $config,
Escaper $escaper,
State $state,
$name = null
) {
$this->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('<info>' . __('The provided rule IDs: %1', '`' . implode(',', $ruleIDs) . '`') . '</info>');
$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();
}
}
11 changes: 10 additions & 1 deletion Model/AutoRelatedProductAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public function __construct(
}
}

public function execute()
/**
* @param array $params
* @return void
*/
public function execute(array $params = []): void
{
$productIdsToCleanCache = [];
$oldProductToRuleData = [];
Expand All @@ -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));

Expand Down
8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,12 @@
</argument>
</arguments>
</type>

<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="mf_arp_apply_rules" xsi:type="object">Magefan\AutoRelatedProduct\Console\Command\ApplyRules</item>
</argument>
</arguments>
</type>
</config>