Skip to content

Commit 84e68af

Browse files
committed
ACP2E-1579: Catalog rule discount disappears after a product is saved from Admin
1 parent 8d6519b commit 84e68af

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Model\Product;
1010
use Magento\Catalog\Model\ProductFactory;
1111
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1213
use Magento\CatalogRule\Model\Indexer\IndexBuilder\ProductLoader;
1314
use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
1415
use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection;
@@ -167,6 +168,11 @@ class IndexBuilder
167168
*/
168169
private $productLoader;
169170

171+
/**
172+
* @var ProductCollectionFactory
173+
*/
174+
private $productCollectionFactory;
175+
170176
/**
171177
* @param RuleCollectionFactory $ruleCollectionFactory
172178
* @param PriceCurrencyInterface $priceCurrency
@@ -188,6 +194,7 @@ class IndexBuilder
188194
* @param ProductLoader|null $productLoader
189195
* @param TableSwapper|null $tableSwapper
190196
* @param TimezoneInterface|null $localeDate
197+
* @param ProductCollectionFactory|null $productCollectionFactory
191198
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
192199
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
193200
*/
@@ -211,7 +218,8 @@ public function __construct(
211218
ActiveTableSwitcher $activeTableSwitcher = null,
212219
ProductLoader $productLoader = null,
213220
TableSwapper $tableSwapper = null,
214-
TimezoneInterface $localeDate = null
221+
TimezoneInterface $localeDate = null,
222+
ProductCollectionFactory $productCollectionFactory = null
215223
) {
216224
$this->resource = $resource;
217225
$this->connection = $resource->getConnection();
@@ -253,6 +261,8 @@ public function __construct(
253261
ObjectManager::getInstance()->get(TableSwapper::class);
254262
$this->localeDate = $localeDate ??
255263
ObjectManager::getInstance()->get(TimezoneInterface::class);
264+
$this->productCollectionFactory = $productCollectionFactory ??
265+
ObjectManager::getInstance()->get(ProductCollectionFactory::class);
256266
}
257267

258268
/**
@@ -497,13 +507,20 @@ protected function applyRule(Rule $rule, $product)
497507
*/
498508
private function applyRules(RuleCollection $ruleCollection, Product $product): void
499509
{
510+
/** @var \Magento\CatalogRule\Model\Rule $rule */
500511
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 = [];
505516
$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)));
507524
}
508525

509526
$this->cleanProductPriceIndex([$product->getId()]);

dev/tests/integration/testsuite/Magento/CatalogRule/Model/Indexer/Product/PriceTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ public function testPriceForSecondStore():void
9696
{
9797
$websiteId = $this->websiteRepository->get('test')->getId();
9898
$simpleProduct = $this->productRepository->get('simple');
99-
$simpleProduct->setPriceCalculation(true);
10099
$this->assertEquals('simple', $simpleProduct->getSku());
101-
$this->assertFalse(
102-
$this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId())
103-
);
104100
$this->indexerBuilder->reindexById($simpleProduct->getId());
105101
$this->assertEquals(
106102
25,

0 commit comments

Comments
 (0)