Skip to content

Commit e388c42

Browse files
committed
ACP2E-1388, refactor solution using an interceptor
1 parent 8618bd6 commit e388c42

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Magento\Bundle\Model\Plugin;
4+
5+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure;
6+
use Magento\CatalogInventory\Model\Indexer\ProductPriceIndexFilter;
7+
use Magento\Framework\App\ResourceConnection;
8+
9+
class ProductPriceIndexModifier
10+
{
11+
/**
12+
* @var ResourceConnection
13+
*/
14+
private ResourceConnection $resourceConnection;
15+
16+
/**
17+
* @var string|null
18+
*/
19+
private ?string $connectionName;
20+
21+
public function __construct(ResourceConnection $resourceConnection, string $connectionName = 'indexer')
22+
{
23+
$this->resourceConnection = $resourceConnection;
24+
$this->connectionName = $connectionName;
25+
}
26+
27+
28+
/**
29+
* Skip entity price index that are part of a dynamic price bundle
30+
*
31+
* @param ProductPriceIndexFilter $subject
32+
* @param callable $proceed
33+
* @param IndexTableStructure $priceTable
34+
* @param array $entityIds
35+
*/
36+
public function aroundModifyPrice(ProductPriceIndexFilter $subject, callable $proceed, IndexTableStructure $priceTable, array $entityIds = [])
37+
{
38+
if (empty($entityIds)) {
39+
$proceed($priceTable, []);
40+
}
41+
42+
foreach ($entityIds as $id) {
43+
if (!$this->isWithinDynamicPriceBundle($priceTable->getTableName(), $id)) {
44+
$proceed($priceTable, [$id]);
45+
}
46+
}
47+
}
48+
49+
/**
50+
* Check if the product is part of a dynamic price bundle configuration
51+
*
52+
* @param string $priceTableName
53+
* @param int $productId
54+
* @return bool
55+
*/
56+
private function isWithinDynamicPriceBundle(string $priceTableName, int $productId): bool
57+
{
58+
$connection = $this->resourceConnection->getConnection($this->connectionName);
59+
$select = $connection->select();
60+
$select->from(['selection' => 'catalog_product_bundle_selection'], 'selection_id');
61+
$select->joinInner(
62+
['entity' => 'catalog_product_entity'],
63+
implode(' AND ', ['selection.parent_product_id = entity.row_id']),
64+
null
65+
);
66+
$select->joinInner(
67+
['price' => $priceTableName],
68+
implode(' AND ', ['price.entity_id = selection.product_id']),
69+
null
70+
);
71+
$select->where('selection.product_id = ?', $productId);
72+
$select->where('entity.type_id = ?', \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE);
73+
$select->where('price.tax_class_id = ?', \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC);
74+
75+
return (int) $connection->fetchOne($select) != 0;
76+
}
77+
}

app/code/Magento/CatalogInventory/Model/Indexer/ProductPriceIndexFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public function modifyPrice(IndexTableStructure $priceTable, array $entityIds =
121121
foreach ($batchSelectIterator as $select) {
122122
$productIds = null;
123123
foreach ($connection->query($select)->fetchAll() as $row) {
124-
if ($row['product_id']) {
125-
$productIds[] = (int) $row['product_id'];
126-
}
124+
$productIds[] = (int) $row['product_id'];
127125
}
128126
if ($productIds !== null) {
129127
$where = [$priceTable->getEntityField() .' IN (?)' => $productIds];

0 commit comments

Comments
 (0)