Skip to content

Commit 260f514

Browse files
authored
Merge pull request #22 from magefan/14008-use-default-stores-for-rule-validation
14008-use-default-stores-for-rule-validation
2 parents d31db51 + 6204b54 commit 260f514

File tree

7 files changed

+147
-26
lines changed

7 files changed

+147
-26
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_VIEWS_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_VIEWS_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: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
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;
22+
use Magento\Store\Model\StoreManagerInterface;
2123

2224
/**
2325
* Class ProductLabelAction
@@ -55,7 +57,7 @@ class ProductLabelAction
5557
private $connection;
5658

5759
/**
58-
* @var \Magefan\ProductLabel\Model\Config
60+
* @var Config
5961
*/
6062
protected $config;
6163

@@ -84,13 +86,19 @@ class ProductLabelAction
8486
*/
8587
private $moduleManager;
8688

89+
/**
90+
* @var StoreManagerInterface
91+
*/
92+
private $storeManager;
93+
8794
/**
8895
* @var null
8996
*/
9097
private $validationFilter = null;
9198

99+
private $allStoreIds = null;
100+
92101
/**
93-
* ProductLabelAction constructor.
94102
* @param RuleCollectionFactory $ruleCollectionFactory
95103
* @param ProductCollectionFactory $productCollectionFactory
96104
* @param CatalogRuleFactory $catalogRuleFactory
@@ -100,7 +108,9 @@ class ProductLabelAction
100108
* @param GetParentProductIdsInterface $getParentProductIds
101109
* @param GetWebsitesMapInterface $getWebsitesMap
102110
* @param ModuleManager $moduleManager
103-
* @param null $validationFilter
111+
* @param Config $config
112+
* @param StoreManagerInterface $storeManager
113+
* @param $validationFilter
104114
*/
105115
public function __construct(
106116
RuleCollectionFactory $ruleCollectionFactory,
@@ -112,6 +122,8 @@ public function __construct(
112122
GetParentProductIdsInterface $getParentProductIds,
113123
GetWebsitesMapInterface $getWebsitesMap,
114124
ModuleManager $moduleManager,
125+
Config $config,
126+
StoreManagerInterface $storeManager,
115127
$validationFilter = null
116128
) {
117129
$this->ruleCollectionFactory = $ruleCollectionFactory;
@@ -123,6 +135,8 @@ public function __construct(
123135
$this->sqlBuilder = $sqlBuilder;
124136
$this->getParentProductIds = $getParentProductIds;
125137
$this->getWebsitesMap = $getWebsitesMap;
138+
$this->config = $config;
139+
$this->storeManager = $storeManager;
126140
$this->moduleManager = $moduleManager;
127141

128142
if ($this->moduleManager->isEnabled('Magefan_DynamicProductAttributes')) {
@@ -235,17 +249,7 @@ public function getListProductIds($rule, array $params = []): array
235249
$conditions = $rule->getConditions();
236250

237251
if (!empty($conditions['conditions'])) {
238-
if ($rule->getWebsiteIds()) {
239-
$storeIds = [];
240-
$websites = $this->getWebsitesMap->execute();
241-
foreach ($websites as $websiteId => $defaultStoreId) {
242-
if (in_array($websiteId, $rule->getWebsiteIds())) {
243-
$storeIds[] = $defaultStoreId;
244-
}
245-
}
246-
} else {
247-
$storeIds = [0];
248-
}
252+
$storeIds = $this->getStoreIdsToValidate($rule);
249253

250254
$conditions = $rule->getConditions()->asArray();
251255

@@ -327,4 +331,75 @@ protected function isRuleWilBeAppliedForSpecificProduct(array $params): bool
327331
{
328332
return $params && isset($params['rule_apply_type']) && ($params['rule_apply_type'] == ApplyByOptions::ON_PRODUCT_SAVE);
329333
}
334+
335+
/**
336+
* @param $rule
337+
* @return array|int[]
338+
* @throws \Magento\Framework\Exception\NoSuchEntityException
339+
*/
340+
private function getStoreIdsToValidate($rule): array
341+
{
342+
$storeIds = [0];
343+
344+
if ($this->config->getRuleValidationScope() === RuleValidationScope::SCOPE_DEFAULT_STORE_VIEWS_PER_WEBSITE) {
345+
$storeIds = $this->getDefaultStoreIdsPerWebsite($rule);
346+
}
347+
348+
if ($this->config->getRuleValidationScope() === RuleValidationScope::SCOPE_SELECTED_STORE_VIEWS_PER_RULE) {
349+
$storeIds = (array)$rule->getStoreIds();
350+
351+
if (empty($storeIds) || in_array(0, $storeIds)) {
352+
$storeIds = $this->getAllStoreIds();
353+
}
354+
}
355+
356+
return $storeIds;
357+
}
358+
359+
360+
/**
361+
* @param $rule
362+
* @return array
363+
* @throws \Magento\Framework\Exception\NoSuchEntityException
364+
*/
365+
private function getDefaultStoreIdsPerWebsite($rule): array
366+
{
367+
$defaultStoreIds = [];
368+
$storeIds = (array)$rule->getStoreIds();
369+
370+
// [website_id => default_store_id]
371+
$websiteToDefaultStoreMap = $this->getWebsitesMap->execute();
372+
373+
if (in_array(0, $storeIds)) {
374+
$defaultStoreIds = $websiteToDefaultStoreMap;
375+
} else {
376+
foreach ($storeIds as $id) {
377+
$websiteId = $this->storeManager->getStore($id)->getWebsiteId();
378+
$websiteIds[$websiteId] = $websiteId;
379+
}
380+
381+
foreach ($websiteIds as $websiteId) {
382+
if (isset($websiteToDefaultStoreMap[$websiteId])) {
383+
$defaultStoreIds[] = $websiteToDefaultStoreMap[$websiteId];
384+
}
385+
}
386+
}
387+
388+
return $defaultStoreIds;
389+
}
390+
391+
/**
392+
* @return array
393+
*/
394+
private function getAllStoreIds()
395+
{
396+
if (null === $this->allStoreIds) {
397+
$this->allStoreIds = [];
398+
399+
foreach ($this->storeManager->getStores() as $store) {
400+
$this->allStoreIds[] = $store->getId();
401+
}
402+
}
403+
return $this->allStoreIds;
404+
}
330405
}

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

etc/di.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
</argument>
1515
</arguments>
1616
</type>
17-
<virtualType name="Magefan\ProductLabel\Model\Store\Ui\Component\Listing\Column\Store" type="Magento\Store\Ui\Component\Listing\Column\Store">
18-
<arguments>
19-
<argument name="storeKey" xsi:type="string">store_id</argument>
20-
</arguments>
21-
</virtualType>
22-
2317

2418
<preference for="Magefan\ProductLabel\Api\RuleRepositoryInterface" type="Magefan\ProductLabel\Model\RuleRepository"/>
2519
<preference for="Magefan\ProductLabel\Api\RuleCollectionInterface" type="Magefan\ProductLabel\Model\ResourceModel\Rule\Collection"/>
@@ -28,13 +22,11 @@
2822
<preference for="Magefan\ProductLabel\Api\Data\RuleSearchResultsInterface" type="Magento\Framework\Api\SearchResults"/>
2923
<preference for="Magefan\ProductLabel\Api\PositionsInterface" type="Magefan\ProductLabel\Model\Config\Source\Positions"/>
3024

31-
3225
<type name="Magento\Framework\Console\CommandList">
3326
<arguments>
3427
<argument name="commands" xsi:type="array">
3528
<item name="Magefan_ProductLabel_ProductLabel" xsi:type="object">Magefan\ProductLabel\Console\Command\ProductLabel</item>
3629
</argument>
3730
</arguments>
3831
</type>
39-
4032
</config>

view/adminhtml/web/js/grid/provider.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ define([
2525
$.extend(params, temp);
2626
});
2727
$.extend(this.params, conditions);
28-
28+
2929
$.extend(this.params, {
30-
'website_ids' : $('[name=website_ids]').val(),
31-
'catalog_price_rule_ids' : $('[name=catalog_price_rule_ids]').val(),
30+
'store_ids' : $('[name=store_ids]').val(),
3231
'apply_by' : $('[name=apply_by]').val(),
3332
'display_on_parent' : $('[name=display_on_parent]').val()
3433
});

0 commit comments

Comments
 (0)