Skip to content

Commit 7c06b74

Browse files
author
Bohdan Berezhniy
committed
14008 added Rule Validation Scope
1 parent 1b79e9a commit 7c06b74

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

Model/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Config
4848

4949
const XML_PATH_CUSTOM_POSITIONS = 'mfproductlabel/general/custom_positions';
5050

51+
const XML_PATH_RULE_VALIDATION_SCOPE = 'mfproductlabel/general/rule_validation_scope';
52+
5153
const SPLITTERS_FOR_CUSTOM_POSITIONS = '<!--mfcp_splitter--!>';
5254

5355
/**
@@ -138,4 +140,12 @@ public function getCustomPositions(): array
138140

139141
return $this->customPositions;
140142
}
143+
144+
/**
145+
* @return int
146+
*/
147+
public function getRuleValidationScope(): int
148+
{
149+
return (int)$this->scopeConfig->getValue(self::XML_PATH_RULE_VALIDATION_SCOPE);
150+
}
141151
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
declare(strict_types=1);
7+
8+
namespace Magefan\ProductLabel\Model\Config\Source;
9+
10+
use Magento\Framework\Data\OptionSourceInterface;
11+
12+
class RuleValidationScope implements OptionSourceInterface
13+
{
14+
public const SCOPE_GLOBAL = 0;
15+
public const SCOPE_DEFAULT_STORE_VIEW_PER_WEBSITE = 1;
16+
public const SCOPE_SELECTED_STORE_VIEWS_PER_RULE = 2;
17+
18+
/**
19+
* @return array[]
20+
*/
21+
public function toOptionArray(): array
22+
{
23+
return [
24+
['value' => self::SCOPE_GLOBAL, 'label' => __('Global (Default)')],
25+
['value' => self::SCOPE_DEFAULT_STORE_VIEW_PER_WEBSITE, 'label' => __('Default Store View(s) per Website')],
26+
['value' => self::SCOPE_SELECTED_STORE_VIEWS_PER_RULE, 'label' => __('Selected Store View(s) per Rule')],
27+
];
28+
}
29+
}

Model/ProductLabelAction.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magefan\Community\Api\GetWebsitesMapInterface;
1919
use Magento\Framework\Module\Manager as ModuleManager;
2020
use Magefan\ProductLabel\Model\Config\Source\ApplyByOptions;
21+
use Magefan\ProductLabel\Model\Config\Source\RuleValidationScope;
2122

2223
/**
2324
* Class ProductLabelAction
@@ -112,6 +113,7 @@ public function __construct(
112113
GetParentProductIdsInterface $getParentProductIds,
113114
GetWebsitesMapInterface $getWebsitesMap,
114115
ModuleManager $moduleManager,
116+
Config $config,
115117
$validationFilter = null
116118
) {
117119
$this->ruleCollectionFactory = $ruleCollectionFactory;
@@ -123,6 +125,7 @@ public function __construct(
123125
$this->sqlBuilder = $sqlBuilder;
124126
$this->getParentProductIds = $getParentProductIds;
125127
$this->getWebsitesMap = $getWebsitesMap;
128+
$this->config = $config;
126129
$this->moduleManager = $moduleManager;
127130

128131
if ($this->moduleManager->isEnabled('Magefan_DynamicProductAttributes')) {
@@ -235,7 +238,8 @@ public function getListProductIds($rule, array $params = []): array
235238
$conditions = $rule->getConditions();
236239

237240
if (!empty($conditions['conditions'])) {
238-
$storeIds = $this->getDefaultStoreIds($rule);
241+
$storeIds = $this->getStoreIdsToValidate($rule);
242+
239243
$conditions = $rule->getConditions()->asArray();
240244

241245
if ($this->validationFilter !== null) {
@@ -317,12 +321,31 @@ protected function isRuleWilBeAppliedForSpecificProduct(array $params): bool
317321
return $params && isset($params['rule_apply_type']) && ($params['rule_apply_type'] == ApplyByOptions::ON_PRODUCT_SAVE);
318322
}
319323

324+
/**
325+
* @param $rule
326+
* @return array|int[]
327+
* @throws \Magento\Framework\Exception\NoSuchEntityException
328+
*/
329+
private function getStoreIdsToValidate($rule): array
330+
{
331+
if ($this->config->getRuleValidationScope() === RuleValidationScope::SCOPE_GLOBAL) {
332+
return [0];
333+
}
334+
335+
if ($this->config->getRuleValidationScope() === RuleValidationScope::SCOPE_DEFAULT_STORE_VIEW_PER_WEBSITE) {
336+
return $this->getDefaultStoreIdsPerWebsite($rule);
337+
}
338+
339+
return (array)$rule->getStoreIds() ?: [0];
340+
}
341+
342+
320343
/**
321344
* @param $rule
322345
* @return array
323346
* @throws \Magento\Framework\Exception\NoSuchEntityException
324347
*/
325-
private function getDefaultStoreIds($rule): array
348+
private function getDefaultStoreIdsPerWebsite($rule): array
326349
{
327350
$defaultStoreIds = [];
328351
$storeIds = (array)$rule->getStoreIds();

etc/adminhtml/system.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@
5656
These positions will be available in the rule, when you choose position "Custom". There you can select the position from this list.
5757
]]></comment>
5858
</field>
59+
60+
<field id="rule_validation_scope" translate="label" type="select" sortOrder="80" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
61+
<label>Rule Validation Scope</label>
62+
<source_model>Magefan\ProductLabel\Model\Config\Source\RuleValidationScope</source_model>
63+
64+
<comment>
65+
<![CDATA[
66+
<p>This setting determines how product conditions are validated for product labels.</p>
67+
<ul>
68+
<li><b>Global (Default):</b> Conditions are validated against the product's global attributes.</li>
69+
<li><b>Default Store View(s) per Website:</b> Conditions are validated using product data from the default store view of each website. This is useful for displaying different labels based on website-specific pricing or stock.</li>
70+
<li><b>Selected Store View(s) per Rule:</b> This allows you to define a specific store view for condition validation, offering the most granular control. <b>Warning: This option can be resource-intensive and may impact performance.</b></li>
71+
</ul>
72+
]]>
73+
</comment>
74+
</field>
5975
</group>
6076
</section>
6177
</system>

etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"_1736868660176_176":{"position":"product_page_before_reviews"}
2626
}
2727
</custom_positions>
28-
28+
<rule_validation_scope>0</rule_validation_scope>
2929
</general>
3030
</mfproductlabel>
3131

0 commit comments

Comments
 (0)