Skip to content

Commit b5bf9d2

Browse files
committed
MAGETWO-93733: [Forwardport] Bundle Product
1 parent f1154d4 commit b5bf9d2

File tree

25 files changed

+2322
-267
lines changed

25 files changed

+2322
-267
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php

Lines changed: 305 additions & 122 deletions
Large diffs are not rendered by default.

app/code/Magento/Downloadable/Model/ResourceModel/Indexer/Price.php

Lines changed: 210 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,176 @@
66
namespace Magento\Downloadable\Model\ResourceModel\Indexer;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier;
11+
use Magento\Downloadable\Model\Product\Type;
12+
use Magento\Eav\Model\Config;
13+
use Magento\Framework\App\ResourceConnection;
14+
use Magento\Framework\Indexer\DimensionalIndexerInterface;
15+
use Magento\Framework\EntityManager\MetadataPool;
16+
use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer;
17+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\BaseFinalPrice;
18+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory;
19+
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure;
920

1021
/**
11-
* Downloadable products Price indexer resource model
12-
*
13-
* @author Magento Core Team <core@magentocommerce.com>
22+
* Downloadable Product Price Indexer Resource model
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1424
*/
15-
class Price extends \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice
25+
class Price implements DimensionalIndexerInterface
1626
{
1727
/**
18-
* @param null|int|array $entityIds
19-
* @return \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice
28+
* @var BaseFinalPrice
2029
*/
21-
protected function reindex($entityIds = null)
22-
{
23-
if ($this->hasEntity() || !empty($entityIds)) {
24-
$this->_prepareFinalPriceData($entityIds);
25-
$this->_applyCustomOption();
26-
$this->_applyDownloadableLink();
27-
$this->_movePriceDataToIndexTable();
28-
}
30+
private $baseFinalPrice;
2931

30-
return $this;
32+
/**
33+
* @var IndexTableStructureFactory
34+
*/
35+
private $indexTableStructureFactory;
36+
37+
/**
38+
* @var TableMaintainer
39+
*/
40+
private $tableMaintainer;
41+
42+
/**
43+
* @var MetadataPool
44+
*/
45+
private $metadataPool;
46+
47+
/**
48+
* @var ResourceConnection
49+
*/
50+
private $resource;
51+
52+
/**
53+
* @var string
54+
*/
55+
private $connectionName;
56+
57+
/**
58+
* @var \Magento\Framework\DB\Adapter\AdapterInterface
59+
*/
60+
private $connection;
61+
62+
/**
63+
* @var Config
64+
*/
65+
private $eavConfig;
66+
67+
/**
68+
* @var BasePriceModifier
69+
*/
70+
private $basePriceModifier;
71+
72+
/**
73+
* @param BaseFinalPrice $baseFinalPrice
74+
* @param IndexTableStructureFactory $indexTableStructureFactory
75+
* @param TableMaintainer $tableMaintainer
76+
* @param MetadataPool $metadataPool
77+
* @param Config $eavConfig
78+
* @param ResourceConnection $resource
79+
* @param BasePriceModifier $basePriceModifier
80+
* @param string $connectionName
81+
*/
82+
public function __construct(
83+
BaseFinalPrice $baseFinalPrice,
84+
IndexTableStructureFactory $indexTableStructureFactory,
85+
TableMaintainer $tableMaintainer,
86+
MetadataPool $metadataPool,
87+
Config $eavConfig,
88+
ResourceConnection $resource,
89+
BasePriceModifier $basePriceModifier,
90+
$connectionName = 'indexer'
91+
) {
92+
$this->baseFinalPrice = $baseFinalPrice;
93+
$this->indexTableStructureFactory = $indexTableStructureFactory;
94+
$this->tableMaintainer = $tableMaintainer;
95+
$this->connectionName = $connectionName;
96+
$this->metadataPool = $metadataPool;
97+
$this->resource = $resource;
98+
$this->eavConfig = $eavConfig;
99+
$this->basePriceModifier = $basePriceModifier;
31100
}
32101

33102
/**
34-
* Retrieve downloadable links price temporary index table name
103+
* {@inheritdoc}
35104
*
36-
* @see _prepareDefaultFinalPriceTable()
37-
*
38-
* @return string
105+
* @throws \Exception
39106
*/
40-
protected function _getDownloadableLinkPriceTable()
107+
public function executeByDimensions(array $dimensions, \Traversable $entityIds)
41108
{
42-
return $this->tableStrategy->getTableName('catalog_product_index_price_downlod');
109+
$temporaryPriceTable = $this->indexTableStructureFactory->create([
110+
'tableName' => $this->tableMaintainer->getMainTmpTable($dimensions),
111+
'entityField' => 'entity_id',
112+
'customerGroupField' => 'customer_group_id',
113+
'websiteField' => 'website_id',
114+
'taxClassField' => 'tax_class_id',
115+
'originalPriceField' => 'price',
116+
'finalPriceField' => 'final_price',
117+
'minPriceField' => 'min_price',
118+
'maxPriceField' => 'max_price',
119+
'tierPriceField' => 'tier_price',
120+
]);
121+
$this->fillFinalPrice($dimensions, $entityIds, $temporaryPriceTable);
122+
$this->basePriceModifier->modifyPrice($temporaryPriceTable, iterator_to_array($entityIds));
123+
$this->applyDownloadableLink($temporaryPriceTable, $dimensions);
43124
}
44125

45126
/**
46-
* Prepare downloadable links price temporary index table
127+
* Calculate and apply Downloadable links price to index
47128
*
129+
* @param IndexTableStructure $temporaryPriceTable
130+
* @param array $dimensions
48131
* @return $this
132+
* @throws \Exception
49133
*/
50-
protected function _prepareDownloadableLinkPriceTable()
51-
{
52-
$this->getConnection()->delete($this->_getDownloadableLinkPriceTable());
134+
private function applyDownloadableLink(
135+
IndexTableStructure $temporaryPriceTable,
136+
array $dimensions
137+
) {
138+
$temporaryDownloadableTableName = 'catalog_product_index_price_downlod_temp';
139+
$this->getConnection()->createTemporaryTableLike(
140+
$temporaryDownloadableTableName,
141+
$this->getTable('catalog_product_index_price_downlod_tmp'),
142+
true
143+
);
144+
$this->fillTemporaryTable($temporaryDownloadableTableName, $dimensions);
145+
$this->updateTemporaryDownloadableTable($temporaryPriceTable->getTableName(), $temporaryDownloadableTableName);
146+
$this->getConnection()->delete($temporaryDownloadableTableName);
53147
return $this;
54148
}
55149

56150
/**
57-
* Calculate and apply Downloadable links price to index
151+
* Retrieve catalog_product attribute instance by attribute code
58152
*
59-
* @return $this
153+
* @param string $attributeCode
154+
* @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
155+
* @throws \Magento\Framework\Exception\LocalizedException
60156
*/
61-
protected function _applyDownloadableLink()
157+
protected function getAttribute($attributeCode)
62158
{
63-
$connection = $this->getConnection();
64-
$table = $this->_getDownloadableLinkPriceTable();
65-
$finalPriceTable = $this->_getDefaultFinalPriceTable();
66-
67-
$this->_prepareDownloadableLinkPriceTable();
68-
69-
$dlType = $this->_getAttribute('links_purchased_separately');
70-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
159+
return $this->eavConfig->getAttribute(Product::ENTITY, $attributeCode);
160+
}
71161

72-
$ifPrice = $connection->getIfNullSql('dlpw.price_id', 'dlpd.price');
162+
/**
163+
* Put data into catalog product price indexer Downloadable links price temp table
164+
*
165+
* @param string $temporaryDownloadableTableName
166+
* @param array $dimensions
167+
* @return void
168+
* @throws \Exception
169+
*/
170+
private function fillTemporaryTable(string $temporaryDownloadableTableName, array $dimensions)
171+
{
172+
$dlType = $this->getAttribute('links_purchased_separately');
173+
$ifPrice = $this->getConnection()->getIfNullSql('dlpw.price_id', 'dlpd.price');
174+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
175+
$linkField = $metadata->getLinkField();
73176

74-
$select = $connection->select()->from(
75-
['i' => $finalPriceTable],
177+
$select = $this->getConnection()->select()->from(
178+
['i' => $this->tableMaintainer->getMainTmpTable($dimensions)],
76179
['entity_id', 'customer_group_id', 'website_id']
77180
)->join(
78181
['dl' => $dlType->getBackend()->getTable()],
@@ -101,30 +204,87 @@ protected function _applyDownloadableLink()
101204
'max_price' => new \Zend_Db_Expr('SUM(' . $ifPrice . ')'),
102205
]
103206
);
207+
$query = $select->insertFromSelect($temporaryDownloadableTableName);
208+
$this->getConnection()->query($query);
209+
}
104210

105-
$query = $select->insertFromSelect($table);
106-
$connection->query($query);
107-
108-
$ifTierPrice = $connection->getCheckSql('i.tier_price IS NOT NULL', '(i.tier_price + id.min_price)', 'NULL');
211+
/**
212+
* Update data in the catalog product price indexer temp table
213+
*
214+
* @param string $temporaryPriceTableName
215+
* @param string $temporaryDownloadableTableName
216+
* @return void
217+
*/
218+
private function updateTemporaryDownloadableTable(
219+
string $temporaryPriceTableName,
220+
string $temporaryDownloadableTableName
221+
) {
222+
$ifTierPrice = $this->getConnection()->getCheckSql(
223+
'i.tier_price IS NOT NULL',
224+
'(i.tier_price + id.min_price)',
225+
'NULL'
226+
);
109227

110-
$select = $connection->select()->join(
111-
['id' => $table],
228+
$selectForCrossUpdate = $this->getConnection()->select()->join(
229+
['id' => $temporaryDownloadableTableName],
112230
'i.entity_id = id.entity_id AND i.customer_group_id = id.customer_group_id' .
113231
' AND i.website_id = id.website_id',
114232
[]
115-
)->columns(
233+
);
234+
// adds price of custom option, that was applied in DefaultPrice::_applyCustomOption
235+
$selectForCrossUpdate->columns(
116236
[
117237
'min_price' => new \Zend_Db_Expr('i.min_price + id.min_price'),
118238
'max_price' => new \Zend_Db_Expr('i.max_price + id.max_price'),
119239
'tier_price' => new \Zend_Db_Expr($ifTierPrice),
120240
]
121241
);
242+
$query = $selectForCrossUpdate->crossUpdateFromSelect(['i' => $temporaryPriceTableName]);
243+
$this->getConnection()->query($query);
244+
}
122245

123-
$query = $select->crossUpdateFromSelect(['i' => $finalPriceTable]);
124-
$connection->query($query);
246+
/**
247+
* Fill final price
248+
*
249+
* @param array $dimensions
250+
* @param \Traversable $entityIds
251+
* @param IndexTableStructure $temporaryPriceTable
252+
* @throws \Magento\Framework\Exception\LocalizedException
253+
* @throws \Zend_Db_Select_Exception
254+
*/
255+
private function fillFinalPrice(
256+
array $dimensions,
257+
\Traversable $entityIds,
258+
IndexTableStructure $temporaryPriceTable
259+
) {
260+
$select = $this->baseFinalPrice->getQuery($dimensions, Type::TYPE_DOWNLOADABLE, iterator_to_array($entityIds));
261+
$query = $select->insertFromSelect($temporaryPriceTable->getTableName(), [], false);
262+
$this->tableMaintainer->getConnection()->query($query);
263+
}
125264

126-
$connection->delete($table);
265+
/**
266+
* Get connection
267+
*
268+
* return \Magento\Framework\DB\Adapter\AdapterInterface
269+
* @throws \DomainException
270+
*/
271+
private function getConnection(): \Magento\Framework\DB\Adapter\AdapterInterface
272+
{
273+
if ($this->connection === null) {
274+
$this->connection = $this->resource->getConnection($this->connectionName);
275+
}
127276

128-
return $this;
277+
return $this->connection;
278+
}
279+
280+
/**
281+
* Get table
282+
*
283+
* @param string $tableName
284+
* @return string
285+
*/
286+
private function getTable($tableName)
287+
{
288+
return $this->resource->getTableName($tableName, $this->connectionName);
129289
}
130290
}

0 commit comments

Comments
 (0)