Skip to content

Commit f1fe1fe

Browse files
committed
MAGETWO-91798: Implement GroupedProduct Price indexer
1 parent c2c3f4c commit f1fe1fe

File tree

3 files changed

+62
-77
lines changed

3 files changed

+62
-77
lines changed

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

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ class Grouped implements DimensionalIndexerInterface
8686
*/
8787
private $fullReindexAction;
8888

89-
/**
90-
* @var ManagerInterface
91-
*/
92-
private $eventManager;
93-
9489
/**
9590
* @param BaseFinalPrice $baseFinalPrice
9691
* @param IndexTableStructureFactory $indexTableStructureFactory
@@ -100,7 +95,6 @@ class Grouped implements DimensionalIndexerInterface
10095
* @param ResourceConnection $resource
10196
* @param string $connectionName
10297
* @param bool $fullReindexAction
103-
* @param ManagerInterface $eventManager
10498
* @param array $priceModifiers
10599
*/
106100
public function __construct(
@@ -112,7 +106,6 @@ public function __construct(
112106
ResourceConnection $resource,
113107
$connectionName = 'indexer',
114108
$fullReindexAction = false,
115-
ManagerInterface $eventManager,
116109
array $priceModifiers = []
117110
) {
118111
$this->baseFinalPrice = $baseFinalPrice;
@@ -124,7 +117,7 @@ public function __construct(
124117
$this->resource = $resource;
125118
$this->eavConfig = $eavConfig;
126119
$this->fullReindexAction = $fullReindexAction;
127-
$this->eventManager = $eventManager;
120+
$this->connection = $this->resource->getConnection($this->connectionName);
128121
}
129122

130123
/**
@@ -148,6 +141,7 @@ private function getMainTable($dimensions)
148141
*/
149142
public function executeByDimension(array $dimensions, \Traversable $entityIds = null)
150143
{
144+
/** @var IndexTableStructure $temporaryPriceTable */
151145
$temporaryPriceTable = $this->indexTableStructureFactory->create([
152146
'tableName' => $this->tableMaintainer->getMainTmpTable($dimensions),
153147
'entityField' => 'entity_id',
@@ -160,22 +154,34 @@ public function executeByDimension(array $dimensions, \Traversable $entityIds =
160154
'maxPriceField' => 'max_price',
161155
'tierPriceField' => 'tier_price',
162156
]);
163-
if (!$this->hasEntity() && empty($entityIds)) {
164-
return $this;
165-
}
157+
$this->fillFinalPrice($dimensions, $entityIds, $temporaryPriceTable);
158+
$this->applyPriceModifiers($temporaryPriceTable);
166159

167160
// if (!$this->tableStrategy->getUseIdxTable()) {
168161
// $additionalIdxTable = $this->cteateTempTable($temporaryPriceTable);
169162
// $this->fillTemporaryTable($entityIds, $additionalIdxTable);
170163
// $this->updateIdxTable($additionalIdxTable, $temporaryPriceTable->getTableName());
171164
// $this->connection->dropTemporaryTable($additionalIdxTable);
172165
// } else {
173-
$query = $this->_prepareGroupedProductPriceDataSelect($dimensions, $entityIds)
166+
$query = $this->_prepareGroupedProductPriceDataSelect($dimensions, iterator_to_array($entityIds))
174167
->insertFromSelect($temporaryPriceTable->getTableName());
175168
$this->connection->query($query);
176169
// }
177170
}
178171

172+
/**
173+
* Apply price modifiers to temporary price index table
174+
*
175+
* @param IndexTableStructure $temporaryPriceTable
176+
* @return void
177+
*/
178+
private function applyPriceModifiers(IndexTableStructure $temporaryPriceTable)
179+
{
180+
foreach ($this->priceModifiers as $priceModifier) {
181+
$priceModifier->modifyPrice($temporaryPriceTable);
182+
}
183+
}
184+
179185
/**
180186
* Prepare data index select for Grouped products prices
181187
* @param $dimensions
@@ -184,52 +190,50 @@ public function executeByDimension(array $dimensions, \Traversable $entityIds =
184190
*/
185191
protected function _prepareGroupedProductPriceDataSelect($dimensions, $entityIds = null)
186192
{
187-
$table = $this->getMainTable($dimensions);
188-
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
193+
$select = $this->connection->select();
189194

190-
$select = $this->connection->select()->from(
195+
$select->from(
191196
['e' => $this->getTable('catalog_product_entity')],
192197
'entity_id'
193-
)->joinLeft(
198+
);
199+
200+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
201+
$select->joinLeft(
194202
['l' => $this->getTable('catalog_product_link')],
195-
'e.' . $linkField . ' = l.product_id AND l.link_type_id=' .
196-
Link::LINK_TYPE_GROUPED,
203+
'e.' . $linkField . ' = l.product_id AND l.link_type_id=' . Link::LINK_TYPE_GROUPED,
197204
[]
198-
)->join(
199-
['cg' => $this->getTable('customer_group')],
200-
'',
201-
['customer_group_id']
202205
);
203-
$this->_addWebsiteJoinToSelect($select, true);
204-
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
205-
$minCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.min_price', 0);
206-
$maxCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.max_price', 0);
207-
$select->columns(
208-
'website_id',
209-
'cw'
210-
)->joinLeft(
206+
//aditional infromation about inner products
207+
$select->joinLeft(
211208
['le' => $this->getTable('catalog_product_entity')],
212209
'le.entity_id = l.linked_product_id',
213210
[]
214-
)->joinLeft(
215-
['i' => $table],
216-
'i.entity_id = l.linked_product_id AND i.website_id = cw.website_id' .
217-
' AND i.customer_group_id = cg.customer_group_id',
211+
);
212+
$select->columns(
213+
[
214+
'i.customer_group_id',
215+
'i.website_id',
216+
]
217+
);
218+
$taxClassId = $this->connection->getCheckSql('MIN(i.tax_class_id) IS NULL', '0', 'MIN(i.tax_class_id)');
219+
$minCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.min_price', 0);
220+
$maxCheckSql = $this->connection->getCheckSql('le.required_options = 0', 'i.max_price', 0);
221+
$select->joinLeft(
222+
['i' => $this->getMainTable($dimensions)],
223+
'i.entity_id = l.linked_product_id',
218224
[
219-
'tax_class_id' => $this->connection->getCheckSql(
220-
'MIN(i.tax_class_id) IS NULL',
221-
'0',
222-
'MIN(i.tax_class_id)'
223-
),
225+
'tax_class_id' => $taxClassId,
224226
'price' => new \Zend_Db_Expr('NULL'),
225227
'final_price' => new \Zend_Db_Expr('NULL'),
226228
'min_price' => new \Zend_Db_Expr('MIN(' . $minCheckSql . ')'),
227229
'max_price' => new \Zend_Db_Expr('MAX(' . $maxCheckSql . ')'),
228230
'tier_price' => new \Zend_Db_Expr('NULL'),
229231
]
230-
)->group(
231-
['e.entity_id', 'cg.customer_group_id', 'cw.website_id']
232-
)->where(
232+
);
233+
$select->group(
234+
['e.entity_id', 'i.customer_group_id', 'i.website_id']
235+
);
236+
$select->where(
233237
'e.type_id=?',
234238
GroupedType::TYPE_CODE
235239
);
@@ -247,18 +251,6 @@ protected function _prepareGroupedProductPriceDataSelect($dimensions, $entityIds
247251
}
248252
}
249253

250-
/**
251-
* Add additional external limitation
252-
*/
253-
$this->eventManager->dispatch(
254-
'catalog_product_prepare_index_select',
255-
[
256-
'select' => $select,
257-
'entity_field' => new \Zend_Db_Expr('e.entity_id'),
258-
'website_field' => new \Zend_Db_Expr('cw.website_id'),
259-
'store_field' => new \Zend_Db_Expr('cs.store_id')
260-
]
261-
);
262254
return $select;
263255
}
264256

@@ -298,30 +290,25 @@ protected function _addWebsiteJoinToSelect($select, $store = true, $joinConditio
298290
return $this;
299291
}
300292

301-
/**
302-
* Add join for catalog/product_website table
303-
* Joined table has alias pw
304-
*
305-
* @param \Magento\Framework\DB\Select $select the select object
306-
* @param string|\Zend_Db_Expr $website the limitation of website_id
307-
* @param string|\Zend_Db_Expr $product the limitation of product_id
308-
* @return $this
309-
*/
310-
protected function _addProductWebsiteJoinToSelect($select, $website, $product)
293+
private function getTable($tableName)
311294
{
312-
$select->join(
313-
['pw' => $this->getTable('catalog_product_website')],
314-
"pw.product_id = {$product} AND pw.website_id = {$website}",
315-
[]
316-
);
317-
318-
return $this;
295+
return $this->resource->getTableName($tableName, $this->connectionName);
319296
}
320297

321-
322-
private function getTable($tableName)
298+
/**
299+
* @param array $dimensions
300+
* @param \Traversable $entityIds
301+
* @param $temporaryPriceTable
302+
*/
303+
private function fillFinalPrice(array $dimensions, \Traversable $entityIds, $temporaryPriceTable): void
323304
{
324-
return $this->resource->getTableName($tableName, $this->connectionName);
305+
$select = $this->baseFinalPrice->getQuery(
306+
$dimensions,
307+
GroupedType::TYPE_CODE,
308+
iterator_to_array($entityIds)
309+
);
310+
$query = $select->insertFromSelect($temporaryPriceTable->getTableName(), [], false);
311+
$this->tableMaintainer->getConnection()->query($query);
325312
}
326313

327314
/**

app/code/Magento/GroupedProduct/etc/di.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
</argument>
8989
</arguments>
9090
</type>
91-
<preference for="Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\GroupedInterface"
92-
type="Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped"/>
9391
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
9492
<arguments>
9593
<argument name="estimators" xsi:type="array">

app/code/Magento/GroupedProduct/etc/product_types.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
99
<type name="grouped" label="Grouped Product" modelInstance="Magento\GroupedProduct\Model\Product\Type\Grouped" composite='true' indexPriority="50" sortOrder="30">
1010
<priceModel instance="Magento\GroupedProduct\Model\Product\Type\Grouped\Price" />
11-
<indexerModel instance="Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\GroupedInterface" />
11+
<indexerModel instance="Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped" />
1212
<stockIndexerModel instance="Magento\GroupedProduct\Model\ResourceModel\Indexer\Stock\Grouped" />
1313
<customAttributes>
1414
<attribute name="is_real_product" value="false"/>

0 commit comments

Comments
 (0)