1818use Magefan \Community \Api \GetWebsitesMapInterface ;
1919use Magento \Framework \Module \Manager as ModuleManager ;
2020use 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}
0 commit comments