18 use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory;
30 private $baseFinalPrice;
35 private $indexTableStructureFactory;
40 private $tableMaintainer;
45 private $metadataPool;
55 private $connectionName;
70 private $basePriceModifier;
84 IndexTableStructureFactory $indexTableStructureFactory,
90 $connectionName =
'indexer' 92 $this->baseFinalPrice = $baseFinalPrice;
93 $this->indexTableStructureFactory = $indexTableStructureFactory;
94 $this->tableMaintainer = $tableMaintainer;
95 $this->connectionName = $connectionName;
96 $this->metadataPool = $metadataPool;
99 $this->basePriceModifier = $basePriceModifier;
110 $temporaryPriceTable = $this->indexTableStructureFactory->create([
111 'tableName' => $this->tableMaintainer->getMainTmpTable($dimensions),
112 'entityField' =>
'entity_id',
113 'customerGroupField' =>
'customer_group_id',
114 'websiteField' =>
'website_id',
115 'taxClassField' =>
'tax_class_id',
116 'originalPriceField' =>
'price',
117 'finalPriceField' =>
'final_price',
118 'minPriceField' =>
'min_price',
119 'maxPriceField' =>
'max_price',
120 'tierPriceField' =>
'tier_price',
122 $this->fillFinalPrice($dimensions, $entityIds, $temporaryPriceTable);
123 $this->basePriceModifier->modifyPrice($temporaryPriceTable, iterator_to_array($entityIds));
124 $this->applyDownloadableLink($temporaryPriceTable, $dimensions);
135 private function applyDownloadableLink(
139 $temporaryDownloadableTableName =
'catalog_product_index_price_downlod_temp';
140 $this->getConnection()->createTemporaryTableLike(
141 $temporaryDownloadableTableName,
142 $this->getTable(
'catalog_product_index_price_downlod_tmp'),
145 $this->fillTemporaryTable($temporaryDownloadableTableName, $dimensions);
146 $this->updateTemporaryDownloadableTable($temporaryPriceTable->
getTableName(), $temporaryDownloadableTableName);
147 $this->getConnection()->delete($temporaryDownloadableTableName);
171 private function fillTemporaryTable(
string $temporaryDownloadableTableName, array $dimensions)
173 $dlType = $this->
getAttribute(
'links_purchased_separately');
174 $ifPrice = $this->getConnection()->getIfNullSql(
'dlpw.price_id',
'dlpd.price');
175 $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
176 $linkField = $metadata->getLinkField();
178 $select = $this->getConnection()->select()->from(
179 [
'i' => $this->tableMaintainer->getMainTmpTable($dimensions)],
180 [
'entity_id',
'customer_group_id',
'website_id']
182 [
'dl' => $dlType->getBackend()->getTable()],
183 "dl.{$linkField} = i.entity_id AND dl.attribute_id = {$dlType->getAttributeId()}" .
" AND dl.store_id = 0",
186 [
'dll' => $this->getTable(
'downloadable_link')],
187 'dll.product_id = i.entity_id',
190 [
'dlpd' => $this->getTable(
'downloadable_link_price')],
191 'dll.link_id = dlpd.link_id AND dlpd.website_id = 0',
194 [
'dlpw' => $this->getTable(
'downloadable_link_price')],
195 'dlpd.link_id = dlpw.link_id AND dlpw.website_id = i.website_id',
201 [
'i.entity_id',
'i.customer_group_id',
'i.website_id']
204 'min_price' =>
new \
Zend_Db_Expr(
'MIN(' . $ifPrice .
')'),
205 'max_price' =>
new \
Zend_Db_Expr(
'SUM(' . $ifPrice .
')'),
208 $query =
$select->insertFromSelect($temporaryDownloadableTableName);
209 $this->getConnection()->query(
$query);
219 private function updateTemporaryDownloadableTable(
220 string $temporaryPriceTableName,
221 string $temporaryDownloadableTableName
223 $ifTierPrice = $this->getConnection()->getCheckSql(
224 'i.tier_price IS NOT NULL',
225 '(i.tier_price + id.min_price)',
229 $selectForCrossUpdate = $this->getConnection()->select()->join(
230 [
'id' => $temporaryDownloadableTableName],
231 'i.entity_id = id.entity_id AND i.customer_group_id = id.customer_group_id' .
232 ' AND i.website_id = id.website_id',
236 $selectForCrossUpdate->columns(
238 'min_price' =>
new \
Zend_Db_Expr(
'i.min_price + id.min_price'),
239 'max_price' =>
new \
Zend_Db_Expr(
'i.max_price + id.max_price'),
243 $query = $selectForCrossUpdate->crossUpdateFromSelect([
'i' => $temporaryPriceTableName]);
244 $this->getConnection()->query(
$query);
256 private function fillFinalPrice(
258 \Traversable $entityIds,
259 IndexTableStructure $temporaryPriceTable
262 $query =
$select->insertFromSelect($temporaryPriceTable->getTableName(), [],
false);
263 $this->tableMaintainer->getConnection()->query(
$query);
272 private function getConnection(): \
Magento\Framework\DB\Adapter\AdapterInterface
274 if ($this->connection ===
null) {
275 $this->connection = $this->resource->getConnection($this->connectionName);
278 return $this->connection;
289 return $this->resource->getTableName(
$tableName, $this->connectionName);
getAttribute($attributeCode)
__construct(BaseFinalPrice $baseFinalPrice, IndexTableStructureFactory $indexTableStructureFactory, TableMaintainer $tableMaintainer, MetadataPool $metadataPool, Config $eavConfig, ResourceConnection $resource, BasePriceModifier $basePriceModifier, $connectionName='indexer')
executeByDimensions(array $dimensions, \Traversable $entityIds)