Skip to content

Commit 92890f9

Browse files
committed
MAGETWO-91798: Implement GroupedProduct Price indexer
1 parent 61d78c1 commit 92890f9

File tree

1 file changed

+48
-76
lines changed
  • app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price

1 file changed

+48
-76
lines changed

app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88
namespace Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11-
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier;
12-
use Magento\Eav\Model\Config;
1311
use Magento\Framework\App\ResourceConnection;
1412
use Magento\Framework\DB\Adapter\AdapterInterface;
1513
use Magento\Framework\Indexer\DimensionalIndexerInterface;
1614
use Magento\Framework\EntityManager\MetadataPool;
1715
use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer;
18-
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\BaseFinalPrice;
1916
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory;
2017
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure;
2118
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\PriceModifierInterface;
22-
use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedType;
2319
use Magento\GroupedProduct\Model\ResourceModel\Product\Link;
2420
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
2521
use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
@@ -61,63 +57,33 @@ class Grouped implements DimensionalIndexerInterface
6157
*/
6258
private $connection;
6359

64-
/**
65-
* @var PriceModifierInterface[]
66-
*/
67-
private $priceModifiers;
68-
6960
/**
7061
* @var bool
7162
*/
7263
private $fullReindexAction;
73-
/**
74-
* @var BasePriceModifier
75-
*/
76-
private $basePriceModifier;
7764

7865
/**
7966
* @param IndexTableStructureFactory $indexTableStructureFactory
8067
* @param TableMaintainer $tableMaintainer
8168
* @param MetadataPool $metadataPool
8269
* @param ResourceConnection $resource
83-
* @param BasePriceModifier $basePriceModifier
8470
* @param string $connectionName
8571
* @param bool $fullReindexAction
86-
* @param array $priceModifiers
8772
*/
8873
public function __construct(
8974
IndexTableStructureFactory $indexTableStructureFactory,
9075
TableMaintainer $tableMaintainer,
9176
MetadataPool $metadataPool,
9277
ResourceConnection $resource,
93-
BasePriceModifier $basePriceModifier,
9478
$connectionName = 'indexer',
95-
$fullReindexAction = false,
96-
array $priceModifiers = []
79+
$fullReindexAction = false
9780
) {
9881
$this->indexTableStructureFactory = $indexTableStructureFactory;
9982
$this->tableMaintainer = $tableMaintainer;
10083
$this->connectionName = $connectionName;
101-
$this->priceModifiers = $priceModifiers;
10284
$this->metadataPool = $metadataPool;
10385
$this->resource = $resource;
10486
$this->fullReindexAction = $fullReindexAction;
105-
$this->connection = $this->resource->getConnection($this->connectionName);
106-
$this->basePriceModifier = $basePriceModifier;
107-
}
108-
109-
/**
110-
* Get main table
111-
*
112-
* @param array $dimensions
113-
* @return string
114-
*/
115-
private function getMainTable($dimensions)
116-
{
117-
if ($this->fullReindexAction) {
118-
return $this->tableMaintainer->getMainReplicaTable($dimensions);
119-
}
120-
return $this->tableMaintainer->getMainTable($dimensions);
12187
}
12288

12389
/**
@@ -140,52 +106,32 @@ public function executeByDimension(array $dimensions, \Traversable $entityIds =
140106
'maxPriceField' => 'max_price',
141107
'tierPriceField' => 'tier_price',
142108
]);
143-
$this->basePriceModifier->modifyPrice($temporaryPriceTable, iterator_to_array($entityIds));
144109
$query = $this->_prepareGroupedProductPriceDataSelect($dimensions, iterator_to_array($entityIds))
145110
->insertFromSelect($temporaryPriceTable->getTableName());
146-
$this->connection->query($query);
111+
$this->getConnection()->query($query);
147112
}
148113

149114
/**
150115
* Prepare data index select for Grouped products prices
151116
* @param $dimensions
152-
* @param int|array $entityIds the parent entity ids limitation
117+
* @param array $entityIds the parent entity ids limitation
153118
* @return \Magento\Framework\DB\Select
154119
*/
155120
protected function _prepareGroupedProductPriceDataSelect($dimensions, $entityIds = null)
156121
{
157-
$select = $this->connection->select();
158-
159-
$select->from(
160-
['e' => $this->getTable('catalog_product_entity')],
161-
'entity_id'
162-
);
122+
$connection = $this->getConnection();
163123

124+
$taxClassId = $connection->getCheckSql('MIN(i.tax_class_id) IS NULL', '0', 'MIN(i.tax_class_id)');
125+
$minCheckSql = $connection->getCheckSql('e.required_options = 0', 'i.min_price', 0);
126+
$maxCheckSql = $connection->getCheckSql('e.required_options = 0', 'i.max_price', 0);
164127
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
165-
$select->joinLeft(
166-
['l' => $this->getTable('catalog_product_link')],
167-
'e.' . $linkField . ' = l.product_id AND l.link_type_id=' . Link::LINK_TYPE_GROUPED,
168-
[]
169-
);
170-
//aditional infromation about inner products
171-
$select->joinLeft(
172-
['le' => $this->getTable('catalog_product_entity')],
173-
'le.entity_id = l.linked_product_id',
174-
[]
175-
);
128+
129+
$select = $connection->select()->from(['l' => $this->getTable('catalog_product_link')], []);
176130
$select->columns(
177131
[
178-
'i.customer_group_id',
179-
'i.website_id',
180-
]
181-
);
182-
$taxClassId = $this->connection->getCheckSql('MIN(i.tax_class_id) IS NULL', '0', 'MIN(i.tax_class_id)');
183-
$minCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.min_price', 0);
184-
$maxCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.max_price', 0);
185-
$select->joinLeft(
186-
['i' => $this->getMainTable($dimensions)],
187-
'i.entity_id = l.linked_product_id',
188-
[
132+
'entity_id' => new \Zend_Db_Expr('l.product_id'),
133+
'customer_group_id' => new \Zend_Db_Expr('i.customer_group_id'),
134+
'website_id' => new \Zend_Db_Expr('i.website_id'),
189135
'tax_class_id' => $taxClassId,
190136
'price' => new \Zend_Db_Expr('NULL'),
191137
'final_price' => new \Zend_Db_Expr('NULL'),
@@ -194,24 +140,21 @@ protected function _prepareGroupedProductPriceDataSelect($dimensions, $entityIds
194140
'tier_price' => new \Zend_Db_Expr('NULL'),
195141
]
196142
);
197-
$select->group(
198-
['e.entity_id', 'i.customer_group_id', 'i.website_id']
199-
);
200-
$select->where(
201-
'e.type_id=?',
202-
GroupedType::TYPE_CODE
203-
);
143+
$select->join(['i' => $this->getMainTable($dimensions)], 'i.entity_id = l.linked_product_id', []);
144+
$select->join(['e' => $this->getTable('catalog_product_entity')], "e.$linkField = l.linked_product_id", []);
145+
$select->where('l.link_type_id = ?', Link::LINK_TYPE_GROUPED);
146+
$select->group(['l.product_id', 'i.customer_group_id', 'i.website_id']);
204147

205148
if ($entityIds !== null) {
206-
$select->where('e.entity_id IN(?)', $entityIds);
149+
$select->where('l.product_id IN(?)', $entityIds);
207150
}
208151

209152
foreach ($dimensions as $dimension) {
210153
if ($dimension->getName() === WebsiteDimensionProvider::DIMENSION_NAME) {
211-
$select->where('`i`.website_id = ?', $dimension->getValue());
154+
$select->where('i.website_id = ?', $dimension->getValue());
212155
}
213156
if ($dimension->getName() === CustomerGroupDimensionProvider::DIMENSION_NAME) {
214-
$select->where('`i`.customer_group_id = ?', $dimension->getValue());
157+
$select->where('i.customer_group_id = ?', $dimension->getValue());
215158
}
216159
}
217160

@@ -222,4 +165,33 @@ private function getTable($tableName)
222165
{
223166
return $this->resource->getTableName($tableName, $this->connectionName);
224167
}
168+
169+
/**
170+
* Get connection
171+
*
172+
* return \Magento\Framework\DB\Adapter\AdapterInterface
173+
* @throws \DomainException
174+
*/
175+
private function getConnection(): \Magento\Framework\DB\Adapter\AdapterInterface
176+
{
177+
if ($this->connection === null) {
178+
$this->connection = $this->resource->getConnection($this->connectionName);
179+
}
180+
181+
return $this->connection;
182+
}
183+
184+
/**
185+
* Get main table
186+
*
187+
* @param array $dimensions
188+
* @return string
189+
*/
190+
private function getMainTable($dimensions)
191+
{
192+
if ($this->fullReindexAction) {
193+
return $this->tableMaintainer->getMainReplicaTable($dimensions);
194+
}
195+
return $this->tableMaintainer->getMainTable($dimensions);
196+
}
225197
}

0 commit comments

Comments
 (0)