Skip to content

Commit c0d9c7a

Browse files
authored
Merge pull request #14 from magefan/12522-aply-specific-rule-via-cli
12522 added abolity to apply scpecific rule via cli
2 parents 4bcaa95 + f062c5b commit c0d9c7a

File tree

2 files changed

+128
-104
lines changed

2 files changed

+128
-104
lines changed

Console/Command/ProductLabel.php

Lines changed: 124 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,124 @@
1-
<?php
2-
/**
3-
* Copyright © Magefan (support@magefan.com). All rights reserved.
4-
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5-
*/
6-
7-
namespace Magefan\ProductLabel\Console\Command;
8-
9-
use Symfony\Component\Console\Command\Command;
10-
use Symfony\Component\Console\Input\InputInterface;
11-
use Symfony\Component\Console\Output\OutputInterface;
12-
use Magefan\ProductLabel\Model\Config\Source\ApplyByOptions;
13-
use Magento\Framework\Escaper;
14-
use Magento\Framework\App\State;
15-
use Magento\Framework\App\Area;
16-
use Magento\Framework\Exception\LocalizedException;
17-
18-
/**
19-
* Class ProductLabel
20-
*/
21-
class ProductLabel extends Command
22-
{
23-
/**
24-
* @var \Magefan\ProductLabel\Model\Config
25-
*/
26-
protected $config;
27-
28-
/**
29-
* @var \Magefan\ProductLabel\Model\ProductLabelAction
30-
*/
31-
protected $productLabelAction;
32-
33-
/**
34-
* @var Escaper
35-
*/
36-
private $escaper;
37-
38-
/**
39-
* @var State
40-
*/
41-
private $state;
42-
43-
/**
44-
* ProductLabel constructor.
45-
* @param \Magefan\ProductLabel\Model\ProductLabelAction $productLabelAction
46-
* @param \Magefan\ProductLabel\Model\Config $config
47-
* @param Escaper $escaper
48-
* @param State $state
49-
* @param null $name
50-
*/
51-
public function __construct(
52-
\Magefan\ProductLabel\Model\ProductLabelAction $productLabelAction,
53-
\Magefan\ProductLabel\Model\Config $config,
54-
Escaper $escaper,
55-
State $state,
56-
$name = null
57-
) {
58-
$this->config = $config;
59-
$this->productLabelAction = $productLabelAction;
60-
$this->escaper = $escaper;
61-
$this->state = $state;
62-
parent::__construct($name);
63-
}
64-
65-
/**
66-
* @param InputInterface $input
67-
* @param OutputInterface $output
68-
* @return int
69-
* @throws \Magento\Framework\Exception\LocalizedException
70-
*/
71-
protected function execute(InputInterface $input, OutputInterface $output): int
72-
{
73-
if ($this->config->isEnabled()) {
74-
try {
75-
$this->state->setAreaCode(Area::AREA_GLOBAL);
76-
} catch (LocalizedException $e) {
77-
$output->writeln((string)__('Something went wrong. %1', $this->escaper->escapeHtml($e->getMessage())));
78-
}
79-
80-
$this->productLabelAction->execute(['rule_apply_type' => ApplyByOptions::MANUALLY]);
81-
$output->writeln(__("Product Label rules have been applied."));
82-
} else {
83-
$output->writeln(__("Product Label extension is disabled. Please turn on it."));
84-
}
85-
return 0;
86-
}
87-
88-
/**
89-
* {@inheritdoc}
90-
*/
91-
protected function configure()
92-
{
93-
$this->setName("magefan:product-label-rules:apply");
94-
$this->setDescription(__("Apply Product Label Rules"));
95-
96-
parent::configure();
97-
}
98-
}
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
namespace Magefan\ProductLabel\Console\Command;
8+
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Magefan\ProductLabel\Model\Config\Source\ApplyByOptions;
14+
use Magento\Framework\Escaper;
15+
use Magento\Framework\App\State;
16+
use Magento\Framework\App\Area;
17+
use Magento\Framework\Exception\LocalizedException;
18+
19+
/**
20+
* Class ProductLabel
21+
*/
22+
class ProductLabel extends Command
23+
{
24+
const RULE_IDS_PARAM = 'ids';
25+
26+
/**
27+
* @var \Magefan\ProductLabel\Model\Config
28+
*/
29+
protected $config;
30+
31+
/**
32+
* @var \Magefan\ProductLabel\Model\ProductLabelAction
33+
*/
34+
protected $productLabelAction;
35+
36+
/**
37+
* @var Escaper
38+
*/
39+
private $escaper;
40+
41+
/**
42+
* @var State
43+
*/
44+
private $state;
45+
46+
/**
47+
* ProductLabel constructor.
48+
* @param \Magefan\ProductLabel\Model\ProductLabelAction $productLabelAction
49+
* @param \Magefan\ProductLabel\Model\Config $config
50+
* @param Escaper $escaper
51+
* @param State $state
52+
* @param null $name
53+
*/
54+
public function __construct(
55+
\Magefan\ProductLabel\Model\ProductLabelAction $productLabelAction,
56+
\Magefan\ProductLabel\Model\Config $config,
57+
Escaper $escaper,
58+
State $state,
59+
$name = null
60+
) {
61+
$this->config = $config;
62+
$this->productLabelAction = $productLabelAction;
63+
$this->escaper = $escaper;
64+
$this->state = $state;
65+
parent::__construct($name);
66+
}
67+
68+
/**
69+
* @param InputInterface $input
70+
* @param OutputInterface $output
71+
* @return int
72+
* @throws \Magento\Framework\Exception\LocalizedException
73+
*/
74+
protected function execute(InputInterface $input, OutputInterface $output): int
75+
{
76+
if ($this->config->isEnabled()) {
77+
try {
78+
$this->state->setAreaCode(Area::AREA_GLOBAL);
79+
} catch (LocalizedException $e) {
80+
$output->writeln((string)__('Something went wrong. %1', $this->escaper->escapeHtml($e->getMessage())));
81+
}
82+
83+
$ruleIDs = (string)$input->getOption(self::RULE_IDS_PARAM);
84+
85+
$ruleIDs = $ruleIDs
86+
? array_map('intval', explode(',', $ruleIDs))
87+
: [];
88+
89+
if ($ruleIDs) {
90+
$output->writeln('<info>' . __('The provided rule IDs: %1', '`' . implode(',', $ruleIDs) . '`') . '</info>');
91+
$this->productLabelAction->execute(['rule_apply_type' => ApplyByOptions::MANUALLY, 'rule_id' => $ruleIDs]);
92+
} else {
93+
$this->productLabelAction->execute(['rule_apply_type' => ApplyByOptions::MANUALLY]);
94+
}
95+
96+
$output->writeln(__("Product Label rules have been applied."));
97+
} else {
98+
$output->writeln(__("Product Label extension is disabled. Please turn on it."));
99+
}
100+
return 0;
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
protected function configure()
107+
{
108+
$options = [
109+
new InputOption(
110+
self::RULE_IDS_PARAM,
111+
null,
112+
InputOption::VALUE_OPTIONAL,
113+
'Rule Ids'
114+
)
115+
];
116+
117+
$this->setDefinition($options);
118+
119+
$this->setName("magefan:product-label-rules:apply");
120+
$this->setDescription(__("Apply Product Label Rules by Rule IDs (comma separated)"));
121+
122+
parent::configure();
123+
}
124+
}

Model/ProductLabelAction.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ public function execute(array $params = [])
146146
->addFieldToFilter('status', 1);
147147

148148
if (isset($params['rule_id'])) {
149-
$ruleId = (int)$params['rule_id'];
150-
if ($ruleId) {
151-
$ruleCollection->addFieldToFilter('id', $ruleId);
152-
}
149+
$ruleIds = (array)$params['rule_id'];
150+
$ruleCollection->addFieldToFilter('id', ['in' => $ruleIds]);
153151
}
154-
152+
155153
if ($ruleCollection) {
156154
if (!$this->isRuleWilBeAppliedForSpecificProduct($params)) {
157155
$select = $this->connection->select()
@@ -325,7 +323,7 @@ private function canApplyRule($rule, array $params = []): bool
325323
* @param array $params
326324
* @return bool
327325
*/
328-
protected function isRuleWilBeAppliedForSpecificProduct(array $params): bool
326+
protected function isRuleWilBeAppliedForSpecificProduct(array $params): bool
329327
{
330328
return $params && isset($params['rule_apply_type']) && ($params['rule_apply_type'] == ApplyByOptions::ON_PRODUCT_SAVE);
331329
}

0 commit comments

Comments
 (0)