|
| 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 | +} |
0 commit comments