9
9
use Magento \Catalog \Model \Product ;
10
10
use Magento \Catalog \Model \ProductFactory ;
11
11
use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
12
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
12
13
use Magento \CatalogRule \Model \Indexer \IndexBuilder \ProductLoader ;
13
14
use Magento \CatalogRule \Model \Indexer \IndexerTableSwapperInterface as TableSwapper ;
14
15
use Magento \CatalogRule \Model \ResourceModel \Rule \Collection as RuleCollection ;
@@ -167,6 +168,11 @@ class IndexBuilder
167
168
*/
168
169
private $ productLoader ;
169
170
171
+ /**
172
+ * @var ProductCollectionFactory
173
+ */
174
+ private $ productCollectionFactory ;
175
+
170
176
/**
171
177
* @param RuleCollectionFactory $ruleCollectionFactory
172
178
* @param PriceCurrencyInterface $priceCurrency
@@ -188,6 +194,7 @@ class IndexBuilder
188
194
* @param ProductLoader|null $productLoader
189
195
* @param TableSwapper|null $tableSwapper
190
196
* @param TimezoneInterface|null $localeDate
197
+ * @param ProductCollectionFactory|null $productCollectionFactory
191
198
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
192
199
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
193
200
*/
@@ -211,7 +218,8 @@ public function __construct(
211
218
ActiveTableSwitcher $ activeTableSwitcher = null ,
212
219
ProductLoader $ productLoader = null ,
213
220
TableSwapper $ tableSwapper = null ,
214
- TimezoneInterface $ localeDate = null
221
+ TimezoneInterface $ localeDate = null ,
222
+ ProductCollectionFactory $ productCollectionFactory = null
215
223
) {
216
224
$ this ->resource = $ resource ;
217
225
$ this ->connection = $ resource ->getConnection ();
@@ -253,6 +261,8 @@ public function __construct(
253
261
ObjectManager::getInstance ()->get (TableSwapper::class);
254
262
$ this ->localeDate = $ localeDate ??
255
263
ObjectManager::getInstance ()->get (TimezoneInterface::class);
264
+ $ this ->productCollectionFactory = $ productCollectionFactory ??
265
+ ObjectManager::getInstance ()->get (ProductCollectionFactory::class);
256
266
}
257
267
258
268
/**
@@ -497,13 +507,20 @@ protected function applyRule(Rule $rule, $product)
497
507
*/
498
508
private function applyRules (RuleCollection $ ruleCollection , Product $ product ): void
499
509
{
510
+ /** @var \Magento\CatalogRule\Model\Rule $rule */
500
511
foreach ($ ruleCollection as $ rule ) {
501
- if (! $ rule -> validate ( $ product )) {
502
- continue ;
503
- }
504
-
512
+ $ productCollection = $ this -> productCollectionFactory -> create ();
513
+ $ productCollection -> addIdFilter ( $ product -> getId ()) ;
514
+ $ rule -> getConditions ()-> collectValidatedAttributes ( $ productCollection );
515
+ $ validationResult = [];
505
516
$ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
506
- $ this ->assignProductToRule ($ rule , $ product ->getId (), $ websiteIds );
517
+ foreach ($ websiteIds as $ websiteId ) {
518
+ $ defaultGroupId = $ this ->storeManager ->getWebsite ($ websiteId )->getDefaultGroupId ();
519
+ $ defaultStoreId = $ this ->storeManager ->getGroup ($ defaultGroupId )->getDefaultStoreId ();
520
+ $ product ->setStoreId ($ defaultStoreId );
521
+ $ validationResult [$ websiteId ] = $ rule ->validate ($ product );
522
+ }
523
+ $ this ->assignProductToRule ($ rule , $ product ->getId (), array_keys (array_filter ($ validationResult )));
507
524
}
508
525
509
526
$ this ->cleanProductPriceIndex ([$ product ->getId ()]);
0 commit comments