Skip to content

Commit 853e180

Browse files
author
Alexander Akimov
authored
Merge pull request #2629 from magento-tsg/2.3-develop-pr20
[TSG] Upporting for 2.3 (pr20) (2.3.0)
2 parents c2b4657 + 9f00b6a commit 853e180

File tree

39 files changed

+1389
-216
lines changed

39 files changed

+1389
-216
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Price/AbstractAction.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ protected function _reindexRows($changedIds = [])
336336
if (!empty($notCompositeIds)) {
337337
$parentProductsTypes = $this->getParentProductsTypes($notCompositeIds);
338338
$productsTypes = array_merge_recursive($productsTypes, $parentProductsTypes);
339-
$parentProductsIds = array_keys($parentProductsTypes);
340-
$compositeIds = $compositeIds + array_combine($parentProductsIds, $parentProductsIds);
341-
$changedIds = array_merge($changedIds, $parentProductsIds);
339+
foreach ($parentProductsTypes as $parentProductsIds) {
340+
$compositeIds = $compositeIds + $parentProductsIds;
341+
$changedIds = array_merge($changedIds, $parentProductsIds);
342+
}
342343
}
343344

344345
if (!empty($compositeIds)) {
@@ -370,7 +371,8 @@ protected function _copyRelationIndexData($parentIds, $excludeIds = null)
370371
['child_id']
371372
)->join(
372373
['e' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
373-
'e.' . $linkField . ' = parent_id'
374+
'e.' . $linkField . ' = parent_id',
375+
[]
374376
)->where(
375377
'e.entity_id IN(?)',
376378
$parentIds

app/code/Magento/Catalog/Model/Indexer/Product/Price/Action/Full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function execute($ids = null)
109109
// Prepare replica table for indexation.
110110
$this->_defaultIndexerResource->getConnection()->truncateTable($replicaTable);
111111

112-
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer $indexer */
112+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice $indexer */
113113
foreach ($this->getTypeIndexers() as $indexer) {
114114
$indexer->getTableStrategy()->setUseIdxTable(false);
115115
$connection = $indexer->getConnection();

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
5252
*/
5353
private $hasEntity = null;
5454

55+
/**
56+
* @var IndexTableStructureFactory
57+
*/
58+
private $indexTableStructureFactory;
59+
60+
/**
61+
* @var PriceModifierInterface[]
62+
*/
63+
private $priceModifiers = [];
64+
5565
/**
5666
* DefaultPrice constructor.
5767
*
@@ -61,19 +71,34 @@ class DefaultPrice extends AbstractIndexer implements PriceInterface
6171
* @param \Magento\Framework\Event\ManagerInterface $eventManager
6272
* @param \Magento\Framework\Module\Manager $moduleManager
6373
* @param string|null $connectionName
64-
* @param null|\Magento\Indexer\Model\Indexer\StateFactory $stateFactory
74+
* @param null|IndexTableStructureFactory $indexTableStructureFactory
75+
* @param PriceModifierInterface[] $priceModifiers
6576
*/
6677
public function __construct(
6778
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6879
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
6980
\Magento\Eav\Model\Config $eavConfig,
7081
\Magento\Framework\Event\ManagerInterface $eventManager,
7182
\Magento\Framework\Module\Manager $moduleManager,
72-
$connectionName = null
83+
$connectionName = null,
84+
IndexTableStructureFactory $indexTableStructureFactory = null,
85+
array $priceModifiers = []
7386
) {
7487
$this->_eventManager = $eventManager;
7588
$this->moduleManager = $moduleManager;
7689
parent::__construct($context, $tableStrategy, $eavConfig, $connectionName);
90+
91+
$this->indexTableStructureFactory = $indexTableStructureFactory ?:
92+
\Magento\Framework\App\ObjectManager::getInstance()->get(IndexTableStructureFactory::class);
93+
foreach ($priceModifiers as $priceModifier) {
94+
if (!($priceModifier instanceof PriceModifierInterface)) {
95+
throw new \InvalidArgumentException(
96+
'Argument \'priceModifiers\' must be of the type ' . PriceModifierInterface::class . '[]'
97+
);
98+
}
99+
100+
$this->priceModifiers[] = $priceModifier;
101+
}
77102
}
78103

79104
/**
@@ -209,13 +234,41 @@ protected function _getDefaultFinalPriceTable()
209234
* Prepare final price temporary index table
210235
*
211236
* @return $this
237+
* @deprecated
238+
* @see prepareFinalPriceTable()
212239
*/
213240
protected function _prepareDefaultFinalPriceTable()
214241
{
215242
$this->getConnection()->delete($this->_getDefaultFinalPriceTable());
216243
return $this;
217244
}
218245

246+
/**
247+
* Create (if needed), clean and return structure of final price table
248+
*
249+
* @return IndexTableStructure
250+
*/
251+
private function prepareFinalPriceTable()
252+
{
253+
$tableName = $this->_getDefaultFinalPriceTable();
254+
$this->getConnection()->delete($tableName);
255+
256+
$finalPriceTable = $this->indexTableStructureFactory->create([
257+
'tableName' => $tableName,
258+
'entityField' => 'entity_id',
259+
'customerGroupField' => 'customer_group_id',
260+
'websiteField' => 'website_id',
261+
'taxClassField' => 'tax_class_id',
262+
'originalPriceField' => 'orig_price',
263+
'finalPriceField' => 'price',
264+
'minPriceField' => 'min_price',
265+
'maxPriceField' => 'max_price',
266+
'tierPriceField' => 'tier_price',
267+
]);
268+
269+
return $finalPriceTable;
270+
}
271+
219272
/**
220273
* Retrieve website current dates table name
221274
*
@@ -248,11 +301,14 @@ protected function _prepareFinalPriceData($entityIds = null)
248301
*/
249302
protected function prepareFinalPriceDataForType($entityIds, $type)
250303
{
251-
$this->_prepareDefaultFinalPriceTable();
304+
$finalPriceTable = $this->prepareFinalPriceTable();
252305

253306
$select = $this->getSelect($entityIds, $type);
254-
$query = $select->insertFromSelect($this->_getDefaultFinalPriceTable(), [], false);
307+
$query = $select->insertFromSelect($finalPriceTable->getTableName(), [], false);
255308
$this->getConnection()->query($query);
309+
310+
$this->applyDiscountPrices($finalPriceTable);
311+
256312
return $this;
257313
}
258314

@@ -359,7 +415,7 @@ protected function getSelect($entityIds = null, $type = null)
359415
'e.' . $metadata->getLinkField(),
360416
'cs.store_id'
361417
);
362-
$currentDate = $connection->getDatePartSql('cwd.website_date');
418+
$currentDate = 'cwd.website_date';
363419

364420
$maxUnsignedBigint = '~0';
365421
$specialFromDate = $connection->getDatePartSql($specialFrom);
@@ -409,6 +465,7 @@ protected function getSelect($entityIds = null, $type = null)
409465
'store_field' => new \Zend_Db_Expr('cs.store_id'),
410466
]
411467
);
468+
412469
return $select;
413470
}
414471

@@ -454,6 +511,19 @@ protected function _prepareCustomOptionPriceTable()
454511
return $this;
455512
}
456513

514+
/**
515+
* Apply discount prices to final price index table.
516+
*
517+
* @param IndexTableStructure $finalPriceTable
518+
* @return void
519+
*/
520+
private function applyDiscountPrices(IndexTableStructure $finalPriceTable) : void
521+
{
522+
foreach ($this->priceModifiers as $priceModifier) {
523+
$priceModifier->modifyPrice($finalPriceTable);
524+
}
525+
}
526+
457527
/**
458528
* Apply custom option minimal and maximal price to temporary final price index table
459529
*
@@ -463,14 +533,15 @@ protected function _prepareCustomOptionPriceTable()
463533
protected function _applyCustomOption()
464534
{
465535
$connection = $this->getConnection();
536+
$finalPriceTable = $this->_getDefaultFinalPriceTable();
466537
$coaTable = $this->_getCustomOptionAggregateTable();
467538
$copTable = $this->_getCustomOptionPriceTable();
468539

469540
$this->_prepareCustomOptionAggregateTable();
470541
$this->_prepareCustomOptionPriceTable();
471542

472543
$select = $connection->select()->from(
473-
['i' => $this->_getDefaultFinalPriceTable()],
544+
['i' => $finalPriceTable],
474545
['entity_id', 'customer_group_id', 'website_id']
475546
)->join(
476547
['cw' => $this->getTable('store_website')],
@@ -537,7 +608,7 @@ protected function _applyCustomOption()
537608
$connection->query($query);
538609

539610
$select = $connection->select()->from(
540-
['i' => $this->_getDefaultFinalPriceTable()],
611+
['i' => $finalPriceTable],
541612
['entity_id', 'customer_group_id', 'website_id']
542613
)->join(
543614
['cw' => $this->getTable('store_website')],
@@ -606,7 +677,7 @@ protected function _applyCustomOption()
606677
$query = $select->insertFromSelect($copTable);
607678
$connection->query($query);
608679

609-
$table = ['i' => $this->_getDefaultFinalPriceTable()];
680+
$table = ['i' => $finalPriceTable];
610681
$select = $connection->select()->join(
611682
['io' => $copTable],
612683
'i.entity_id = io.entity_id AND i.customer_group_id = io.customer_group_id' .
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
9+
10+
/**
11+
* Wrapper for structure of price index table.
12+
*/
13+
class IndexTableStructure
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $tableName;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $entityField;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $customerGroupField;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $websiteField;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $taxClassField;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $originalPriceField;
44+
45+
/**
46+
* @var string
47+
*/
48+
private $finalPriceField;
49+
50+
/**
51+
* @var string
52+
*/
53+
private $minPriceField;
54+
55+
/**
56+
* @var string
57+
*/
58+
private $maxPriceField;
59+
60+
/**
61+
* @var string
62+
*/
63+
private $tierPriceField;
64+
65+
/**
66+
* @param string $tableName
67+
* @param string $entityField
68+
* @param string $customerGroupField
69+
* @param string $websiteField
70+
* @param string $taxClassField
71+
* @param string $originalPriceField
72+
* @param string $finalPriceField
73+
* @param string $minPriceField
74+
* @param string $maxPriceField
75+
* @param string $tierPriceField
76+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
77+
*/
78+
public function __construct(
79+
string $tableName,
80+
string $entityField,
81+
string $customerGroupField,
82+
string $websiteField,
83+
string $taxClassField,
84+
string $originalPriceField,
85+
string $finalPriceField,
86+
string $minPriceField,
87+
string $maxPriceField,
88+
string $tierPriceField
89+
) {
90+
$this->tableName = $tableName;
91+
$this->entityField = $entityField;
92+
$this->customerGroupField = $customerGroupField;
93+
$this->websiteField = $websiteField;
94+
$this->taxClassField = $taxClassField;
95+
$this->originalPriceField = $originalPriceField;
96+
$this->finalPriceField = $finalPriceField;
97+
$this->minPriceField = $minPriceField;
98+
$this->maxPriceField = $maxPriceField;
99+
$this->tierPriceField = $tierPriceField;
100+
}
101+
102+
/**
103+
* @return string
104+
*/
105+
public function getTableName(): string
106+
{
107+
return $this->tableName;
108+
}
109+
110+
/**
111+
* @return string
112+
*/
113+
public function getEntityField(): string
114+
{
115+
return $this->entityField;
116+
}
117+
118+
/**
119+
* @return string
120+
*/
121+
public function getCustomerGroupField(): string
122+
{
123+
return $this->customerGroupField;
124+
}
125+
126+
/**
127+
* @return string
128+
*/
129+
public function getWebsiteField(): string
130+
{
131+
return $this->websiteField;
132+
}
133+
134+
/**
135+
* @return string
136+
*/
137+
public function getTaxClassField(): string
138+
{
139+
return $this->taxClassField;
140+
}
141+
142+
/**
143+
* @return string
144+
*/
145+
public function getOriginalPriceField(): string
146+
{
147+
return $this->originalPriceField;
148+
}
149+
150+
/**
151+
* @return string
152+
*/
153+
public function getFinalPriceField(): string
154+
{
155+
return $this->finalPriceField;
156+
}
157+
158+
/**
159+
* @return string
160+
*/
161+
public function getMinPriceField(): string
162+
{
163+
return $this->minPriceField;
164+
}
165+
166+
/**
167+
* @return string
168+
*/
169+
public function getMaxPriceField(): string
170+
{
171+
return $this->maxPriceField;
172+
}
173+
174+
/**
175+
* @return string
176+
*/
177+
public function getTierPriceField(): string
178+
{
179+
return $this->tierPriceField;
180+
}
181+
}

0 commit comments

Comments
 (0)