Skip to content

Commit 3ae8fe1

Browse files
committed
Merge remote-tracking branch 'mainline/2.4-develop' into PR_20210609
2 parents 30b8eaa + 912857f commit 3ae8fe1

File tree

2 files changed

+141
-44
lines changed

2 files changed

+141
-44
lines changed

app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Indexer/ProductRuleReindex.php

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
*/
77
namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Indexer;
88

9+
use Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer;
910
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1011
use Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ConfigurableProductsProvider;
1112

1213
/**
13-
* Class ReindexProduct. Add configurable sub-products to reindex
14+
* Add configurable sub-products to reindex
1415
*/
1516
class ProductRuleReindex
1617
{
1718
/**
18-
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
19+
* @var Configurable
1920
*/
2021
private $configurable;
2122

2223
/**
23-
* @var \Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ConfigurableProductsProvider
24+
* @var ConfigurableProductsProvider
2425
*/
2526
private $configurableProductsProvider;
2627

@@ -37,61 +38,47 @@ public function __construct(
3738
}
3839

3940
/**
40-
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
41+
* Reindex configurable product with sub-products
42+
*
43+
* @param ProductRuleIndexer $subject
4144
* @param \Closure $proceed
4245
* @param int $id
43-
*
4446
* @return void
4547
*/
46-
public function aroundExecuteRow(
47-
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject,
48-
\Closure $proceed,
49-
$id
50-
) {
48+
public function aroundExecuteRow(ProductRuleIndexer $subject, \Closure $proceed, $id)
49+
{
50+
$isReindexed = false;
51+
5152
$configurableProductIds = $this->configurableProductsProvider->getIds([$id]);
52-
$this->reindexSubProducts($configurableProductIds, $subject);
53-
if (!$configurableProductIds) {
54-
$proceed($id);
53+
if ($configurableProductIds) {
54+
$subProducts = array_values($this->configurable->getChildrenIds($id)[0]);
55+
if ($subProducts) {
56+
$subject->executeList(array_merge([$id], $subProducts));
57+
$isReindexed = true;
58+
}
5559
}
56-
}
5760

58-
/**
59-
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
60-
* @param \Closure $proceed
61-
* @param array $ids
62-
*
63-
* @return void
64-
*/
65-
public function aroundExecuteList(
66-
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject,
67-
\Closure $proceed,
68-
array $ids
69-
) {
70-
$configurableProductIds = $this->configurableProductsProvider->getIds($ids);
71-
$subProducts = $this->reindexSubProducts($configurableProductIds, $subject);
72-
$ids = array_diff($ids, $configurableProductIds, $subProducts);
73-
if ($ids) {
74-
$proceed($ids);
61+
if (!$isReindexed) {
62+
$proceed($id);
7563
}
7664
}
7765

7866
/**
79-
* @param array $configurableIds
80-
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
67+
* Add sub-products to reindex
8168
*
69+
* @param ProductRuleIndexer $subject
70+
* @param array $ids
8271
* @return array
72+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8373
*/
84-
private function reindexSubProducts(
85-
array $configurableIds,
86-
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
87-
) {
88-
$subProducts = [];
89-
if ($configurableIds) {
90-
$subProducts = array_values($this->configurable->getChildrenIds($configurableIds)[0]);
91-
if ($subProducts) {
92-
$subject->executeList($subProducts);
93-
}
74+
public function beforeExecuteList(ProductRuleIndexer $subject, array $ids): array
75+
{
76+
$configurableProductIds = $this->configurableProductsProvider->getIds($ids);
77+
if ($configurableProductIds) {
78+
$subProducts = array_values($this->configurable->getChildrenIds($configurableProductIds)[0]);
79+
$ids = array_unique(array_merge($ids, $subProducts));
9480
}
95-
return $subProducts;
81+
82+
return [$ids];
9683
}
9784
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRuleConfigurable\Model\Indexer\Product;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer;
13+
use Magento\CatalogRule\Pricing\Price\CatalogRulePrice;
14+
use Magento\Framework\Pricing\Price\Factory as PriceFactory;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @magentoDbIsolation disabled
20+
* @magentoAppArea adminhtml
21+
* @magentoDataFixture Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rule.php
22+
*/
23+
class ProductRuleIndexerTest extends TestCase
24+
{
25+
/**
26+
* @var ProductRepositoryInterface
27+
*/
28+
private $productRepository;
29+
30+
/**
31+
* @var PriceFactory
32+
*/
33+
private $priceFactory;
34+
35+
/**
36+
* @var ProductRuleIndexer
37+
*/
38+
private $productRuleIndexer;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
$objectManager = Bootstrap::getObjectManager();
46+
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
47+
$this->priceFactory = $objectManager->get(PriceFactory::class);
48+
$this->productRuleIndexer = $objectManager->create(ProductRuleIndexer::class);
49+
}
50+
51+
/**
52+
* @return void
53+
*/
54+
public function testExecute(): void
55+
{
56+
$product = $this->productRepository->get('configurable');
57+
$this->productRuleIndexer->execute([$product->getId()]);
58+
59+
$product = $this->productRepository->get('simple_10');
60+
$price = $this->getCatalogRulePrice($product);
61+
$this->assertEquals(5, $price);
62+
}
63+
64+
/**
65+
* @return void
66+
*/
67+
public function testExecuteRow(): void
68+
{
69+
$product = $this->productRepository->get('configurable');
70+
$this->productRuleIndexer->executeRow($product->getId());
71+
72+
$product = $this->productRepository->get('simple_10');
73+
$price = $this->getCatalogRulePrice($product);
74+
$this->assertEquals(5, $price);
75+
}
76+
77+
/**
78+
* @return void
79+
*/
80+
public function testExecuteList(): void
81+
{
82+
$product = $this->productRepository->get('configurable');
83+
$this->productRuleIndexer->executeList([$product->getId()]);
84+
85+
$product = $this->productRepository->get('simple_10');
86+
$price = $this->getCatalogRulePrice($product);
87+
$this->assertEquals(5, $price);
88+
}
89+
90+
public function testExecuteFull(): void
91+
{
92+
$this->productRuleIndexer->executeFull();
93+
94+
$product = $this->productRepository->get('simple_10');
95+
$price = $this->getCatalogRulePrice($product);
96+
$this->assertEquals(5, $price);
97+
}
98+
99+
/**
100+
* @param Product $product
101+
* @return float|bool
102+
*/
103+
private function getCatalogRulePrice(Product $product)
104+
{
105+
$catalogRulePrice = $this->priceFactory->create($product, CatalogRulePrice::class, 1);
106+
$price = $catalogRulePrice->getValue();
107+
108+
return $price;
109+
}
110+
}

0 commit comments

Comments
 (0)