|
6 | 6 | namespace Magento\Downloadable\Model\ResourceModel\Indexer;
|
7 | 7 |
|
8 | 8 | 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; |
9 | 20 |
|
10 | 21 | /**
|
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) |
14 | 24 | */
|
15 |
| -class Price extends \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice |
| 25 | +class Price implements DimensionalIndexerInterface |
16 | 26 | {
|
17 | 27 | /**
|
18 |
| - * @param null|int|array $entityIds |
19 |
| - * @return \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice |
| 28 | + * @var BaseFinalPrice |
20 | 29 | */
|
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; |
29 | 31 |
|
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; |
31 | 100 | }
|
32 | 101 |
|
33 | 102 | /**
|
34 |
| - * Retrieve downloadable links price temporary index table name |
| 103 | + * {@inheritdoc} |
35 | 104 | *
|
36 |
| - * @see _prepareDefaultFinalPriceTable() |
37 |
| - * |
38 |
| - * @return string |
| 105 | + * @throws \Exception |
39 | 106 | */
|
40 |
| - protected function _getDownloadableLinkPriceTable() |
| 107 | + public function executeByDimensions(array $dimensions, \Traversable $entityIds) |
41 | 108 | {
|
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); |
43 | 124 | }
|
44 | 125 |
|
45 | 126 | /**
|
46 |
| - * Prepare downloadable links price temporary index table |
| 127 | + * Calculate and apply Downloadable links price to index |
47 | 128 | *
|
| 129 | + * @param IndexTableStructure $temporaryPriceTable |
| 130 | + * @param array $dimensions |
48 | 131 | * @return $this
|
| 132 | + * @throws \Exception |
49 | 133 | */
|
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); |
53 | 147 | return $this;
|
54 | 148 | }
|
55 | 149 |
|
56 | 150 | /**
|
57 |
| - * Calculate and apply Downloadable links price to index |
| 151 | + * Retrieve catalog_product attribute instance by attribute code |
58 | 152 | *
|
59 |
| - * @return $this |
| 153 | + * @param string $attributeCode |
| 154 | + * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute |
| 155 | + * @throws \Magento\Framework\Exception\LocalizedException |
60 | 156 | */
|
61 |
| - protected function _applyDownloadableLink() |
| 157 | + protected function getAttribute($attributeCode) |
62 | 158 | {
|
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 | + } |
71 | 161 |
|
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(); |
73 | 176 |
|
74 |
| - $select = $connection->select()->from( |
75 |
| - ['i' => $finalPriceTable], |
| 177 | + $select = $this->getConnection()->select()->from( |
| 178 | + ['i' => $this->tableMaintainer->getMainTmpTable($dimensions)], |
76 | 179 | ['entity_id', 'customer_group_id', 'website_id']
|
77 | 180 | )->join(
|
78 | 181 | ['dl' => $dlType->getBackend()->getTable()],
|
@@ -101,30 +204,87 @@ protected function _applyDownloadableLink()
|
101 | 204 | 'max_price' => new \Zend_Db_Expr('SUM(' . $ifPrice . ')'),
|
102 | 205 | ]
|
103 | 206 | );
|
| 207 | + $query = $select->insertFromSelect($temporaryDownloadableTableName); |
| 208 | + $this->getConnection()->query($query); |
| 209 | + } |
104 | 210 |
|
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 | + ); |
109 | 227 |
|
110 |
| - $select = $connection->select()->join( |
111 |
| - ['id' => $table], |
| 228 | + $selectForCrossUpdate = $this->getConnection()->select()->join( |
| 229 | + ['id' => $temporaryDownloadableTableName], |
112 | 230 | 'i.entity_id = id.entity_id AND i.customer_group_id = id.customer_group_id' .
|
113 | 231 | ' AND i.website_id = id.website_id',
|
114 | 232 | []
|
115 |
| - )->columns( |
| 233 | + ); |
| 234 | + // adds price of custom option, that was applied in DefaultPrice::_applyCustomOption |
| 235 | + $selectForCrossUpdate->columns( |
116 | 236 | [
|
117 | 237 | 'min_price' => new \Zend_Db_Expr('i.min_price + id.min_price'),
|
118 | 238 | 'max_price' => new \Zend_Db_Expr('i.max_price + id.max_price'),
|
119 | 239 | 'tier_price' => new \Zend_Db_Expr($ifTierPrice),
|
120 | 240 | ]
|
121 | 241 | );
|
| 242 | + $query = $selectForCrossUpdate->crossUpdateFromSelect(['i' => $temporaryPriceTableName]); |
| 243 | + $this->getConnection()->query($query); |
| 244 | + } |
122 | 245 |
|
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 | + } |
125 | 264 |
|
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 | + } |
127 | 276 |
|
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); |
129 | 289 | }
|
130 | 290 | }
|
0 commit comments