1919use Magento \Framework \Module \Manager as ModuleManager ;
2020use Magefan \ProductLabel \Model \Config \Source \ApplyByOptions ;
2121use Magefan \ProductLabel \Model \Config \Source \RuleValidationScope ;
22+ use Magento \Store \Model \StoreManagerInterface ;
2223
2324/**
2425 * Class ProductLabelAction
@@ -56,7 +57,7 @@ class ProductLabelAction
5657 private $ connection ;
5758
5859 /**
59- * @var \Magefan\ProductLabel\Model\ Config
60+ * @var Config
6061 */
6162 protected $ config ;
6263
@@ -85,13 +86,19 @@ class ProductLabelAction
8586 */
8687 private $ moduleManager ;
8788
89+ /**
90+ * @var StoreManagerInterface
91+ */
92+ private $ storeManager ;
93+
8894 /**
8995 * @var null
9096 */
9197 private $ validationFilter = null ;
9298
99+ private $ allStoreIds = null ;
100+
93101 /**
94- * ProductLabelAction constructor.
95102 * @param RuleCollectionFactory $ruleCollectionFactory
96103 * @param ProductCollectionFactory $productCollectionFactory
97104 * @param CatalogRuleFactory $catalogRuleFactory
@@ -101,7 +108,9 @@ class ProductLabelAction
101108 * @param GetParentProductIdsInterface $getParentProductIds
102109 * @param GetWebsitesMapInterface $getWebsitesMap
103110 * @param ModuleManager $moduleManager
104- * @param null $validationFilter
111+ * @param Config $config
112+ * @param StoreManagerInterface $storeManager
113+ * @param $validationFilter
105114 */
106115 public function __construct (
107116 RuleCollectionFactory $ ruleCollectionFactory ,
@@ -114,6 +123,7 @@ public function __construct(
114123 GetWebsitesMapInterface $ getWebsitesMap ,
115124 ModuleManager $ moduleManager ,
116125 Config $ config ,
126+ StoreManagerInterface $ storeManager ,
117127 $ validationFilter = null
118128 ) {
119129 $ this ->ruleCollectionFactory = $ ruleCollectionFactory ;
@@ -126,6 +136,7 @@ public function __construct(
126136 $ this ->getParentProductIds = $ getParentProductIds ;
127137 $ this ->getWebsitesMap = $ getWebsitesMap ;
128138 $ this ->config = $ config ;
139+ $ this ->storeManager = $ storeManager ;
129140 $ this ->moduleManager = $ moduleManager ;
130141
131142 if ($ this ->moduleManager ->isEnabled ('Magefan_DynamicProductAttributes ' )) {
@@ -328,15 +339,21 @@ protected function isRuleWilBeAppliedForSpecificProduct(array $params): bool
328339 */
329340 private function getStoreIdsToValidate ($ rule ): array
330341 {
331- if ($ this ->config ->getRuleValidationScope () === RuleValidationScope::SCOPE_GLOBAL ) {
332- return [0 ];
342+ $ storeIds = [0 ];
343+
344+ if ($ this ->config ->getRuleValidationScope () === RuleValidationScope::SCOPE_DEFAULT_STORE_VIEWS_PER_WEBSITE ) {
345+ $ storeIds = $ this ->getDefaultStoreIdsPerWebsite ($ rule );
333346 }
334347
335- if ($ this ->config ->getRuleValidationScope () === RuleValidationScope::SCOPE_DEFAULT_STORE_VIEW_PER_WEBSITE ) {
336- return $ this ->getDefaultStoreIdsPerWebsite ($ rule );
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+ }
337354 }
338355
339- return ( array ) $ rule -> getStoreIds () ?: [ 0 ] ;
356+ return $ storeIds ;
340357 }
341358
342359
@@ -356,10 +373,8 @@ private function getDefaultStoreIdsPerWebsite($rule): array
356373 if (in_array (0 , $ storeIds )) {
357374 $ defaultStoreIds = $ websiteToDefaultStoreMap ;
358375 } else {
359- $ storeManager = \Magento \Framework \App \ObjectManager::getInstance ()->get (\Magento \Store \Model \StoreManagerInterface::class);
360-
361376 foreach ($ storeIds as $ id ) {
362- $ websiteId = $ storeManager ->getStore ($ id )->getWebsiteId ();
377+ $ websiteId = $ this -> storeManager ->getStore ($ id )->getWebsiteId ();
363378 $ websiteIds [$ websiteId ] = $ websiteId ;
364379 }
365380
@@ -372,4 +387,19 @@ private function getDefaultStoreIdsPerWebsite($rule): array
372387
373388 return $ defaultStoreIds ;
374389 }
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+ }
375405}
0 commit comments