Skip to content

Commit 400f4ee

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #1162 from magento-tsg/2.1.8-develop-pr16
[TSG] Backporting for 2.1 (pr16) (2.1.8)
2 parents 88ed064 + 58d13c8 commit 400f4ee

File tree

79 files changed

+2843
-754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2843
-754
lines changed

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,24 @@ protected function retrieveOldSkus()
542542
*/
543543
protected function processCountExistingPrices($prices, $table)
544544
{
545+
$oldSkus = $this->retrieveOldSkus();
546+
$existProductIds = array_intersect_key($oldSkus, $prices);
547+
if (!count($existProductIds)) {
548+
return $this;
549+
}
550+
545551
$tableName = $this->_resourceFactory->create()->getTable($table);
546552
$productEntityLinkField = $this->getProductEntityLinkField();
547553
$existingPrices = $this->_connection->fetchAssoc(
548554
$this->_connection->select()->from(
549555
$tableName,
550556
['value_id', $productEntityLinkField, 'all_groups', 'customer_group_id']
551-
)
557+
)->where($productEntityLinkField . ' IN (?)', $existProductIds)
552558
);
553-
$oldSkus = $this->retrieveOldSkus();
554559
foreach ($existingPrices as $existingPrice) {
555-
foreach ($oldSkus as $sku => $productId) {
556-
if ($existingPrice[$productEntityLinkField] == $productId && isset($prices[$sku])) {
557-
$this->incrementCounterUpdated($prices[$sku], $existingPrice);
560+
foreach ($prices as $sku => $skuPrices) {
561+
if (isset($oldSkus[$sku]) && $existingPrice[$productEntityLinkField] == $oldSkus[$sku]) {
562+
$this->incrementCounterUpdated($skuPrices, $existingPrice);
558563
}
559564
}
560565
}

app/code/Magento/Catalog/Model/Product/Copier.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111

12+
/**
13+
* Catalog product copier.
14+
*/
1215
class Copier
1316
{
1417
/**
@@ -54,12 +57,15 @@ public function copy(\Magento\Catalog\Model\Product $product)
5457
$product->getWebsiteIds();
5558
$product->getCategoryIds();
5659

60+
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
61+
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
62+
5763
/** @var \Magento\Catalog\Model\Product $duplicate */
5864
$duplicate = $this->productFactory->create();
5965
$duplicate->setData($product->getData());
6066
$duplicate->setOptions([]);
6167
$duplicate->setIsDuplicate(true);
62-
$duplicate->setOriginalId($product->getEntityId());
68+
$duplicate->setOriginalLinkId($product->getData($metadata->getLinkField()));
6369
$duplicate->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
6470
$duplicate->setCreatedAt(null);
6571
$duplicate->setUpdatedAt(null);
@@ -81,11 +87,11 @@ public function copy(\Magento\Catalog\Model\Product $product)
8187
}
8288
} while (!$isDuplicateSaved);
8389
$this->getOptionRepository()->duplicate($product, $duplicate);
84-
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
8590
$product->getResource()->duplicate(
8691
$product->getData($metadata->getLinkField()),
8792
$duplicate->getData($metadata->getLinkField())
8893
);
94+
8995
return $duplicate;
9096
}
9197

@@ -97,8 +103,9 @@ private function getOptionRepository()
97103
{
98104
if (null === $this->optionRepository) {
99105
$this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance()
100-
->get('Magento\Catalog\Model\Product\Option\Repository');
106+
->get(\Magento\Catalog\Model\Product\Option\Repository::class);
101107
}
108+
102109
return $this->optionRepository;
103110
}
104111

@@ -110,8 +117,9 @@ private function getMetadataPool()
110117
{
111118
if (null === $this->metadataPool) {
112119
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
113-
->get('Magento\Framework\EntityManager\MetadataPool');
120+
->get(\Magento\Framework\EntityManager\MetadataPool::class);
114121
}
122+
115123
return $this->metadataPool;
116124
}
117125
}

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ protected function processNewImage($product, array &$image)
278278
}
279279

280280
/**
281+
* Duplicate product media gallery data.
282+
*
281283
* @param \Magento\Catalog\Model\Product $product
282284
* @return $this
283285
*/
@@ -294,7 +296,7 @@ protected function duplicate($product)
294296
$this->resourceModel->duplicate(
295297
$this->getAttribute()->getAttributeId(),
296298
isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [],
297-
$product->getOriginalId(),
299+
$product->getOriginalLinkId(),
298300
$product->getData($this->metadata->getLinkField())
299301
);
300302

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,16 +2213,24 @@ public function addMediaGalleryData()
22132213
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
22142214
$items = $this->getItems();
22152215

2216-
$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
2217-
return $item->getId();
2218-
}, $items));
2216+
$select->where(
2217+
'entity.' . $linkField . ' IN (?)',
2218+
array_map(
2219+
function ($item) use ($linkField) {
2220+
return $item->getData($linkField);
2221+
},
2222+
$items
2223+
)
2224+
);
22192225

22202226
foreach ($this->getConnection()->fetchAll($select) as $row) {
22212227
$mediaGalleries[$row[$linkField]][] = $row;
22222228
}
22232229

22242230
foreach ($items as $item) {
2225-
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
2231+
$mediaEntries = isset($mediaGalleries[$item->getData($linkField)])
2232+
? $mediaGalleries[$item->getData($linkField)]
2233+
: [];
22262234
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
22272235
}
22282236

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,25 @@ protected function _prepareRelationIndexSelect($parentIds = null)
197197
)->joinLeft(
198198
['e' => $this->getTable('catalog_product_entity')],
199199
'e.' . $linkField .' = l.parent_id',
200-
['e.entity_id as parent_id']
200+
[]
201201
)->join(
202202
['cs' => $this->getTable('store')],
203203
'',
204204
[]
205205
)->join(
206206
['i' => $idxTable],
207207
'l.child_id = i.entity_id AND cs.store_id = i.store_id',
208-
['attribute_id', 'store_id', 'value']
208+
[]
209209
)->group(
210-
['parent_id', 'i.attribute_id', 'i.store_id', 'i.value']
210+
['parent_id', 'i.attribute_id', 'i.store_id', 'i.value', 'l.child_id']
211+
)->columns(
212+
[
213+
'parent_id' => 'e.entity_id',
214+
'attribute_id' => 'i.attribute_id',
215+
'store_id' => 'i.store_id',
216+
'value' => 'i.value',
217+
'source_id' => 'l.child_id'
218+
]
211219
);
212220
if ($parentIds !== null) {
213221
$select->where('e.entity_id IN(?)', $parentIds);
@@ -222,7 +230,7 @@ protected function _prepareRelationIndexSelect($parentIds = null)
222230
'select' => $select,
223231
'entity_field' => new \Zend_Db_Expr('l.parent_id'),
224232
'website_field' => new \Zend_Db_Expr('cs.website_id'),
225-
'store_field' => new \Zend_Db_Expr('cs.store_id')
233+
'store_field' => new \Zend_Db_Expr('cs.store_id'),
226234
]
227235
);
228236

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null)
8585
'pdd.attribute_id',
8686
'cs.store_id',
8787
'value' => $productValueExpression,
88+
'source_id' => 'cpe.entity_id',
8889
]
8990
);
9091

@@ -116,7 +117,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null)
116117
'select' => $select,
117118
'entity_field' => new \Zend_Db_Expr('cpe.entity_id'),
118119
'website_field' => new \Zend_Db_Expr('cs.website_id'),
119-
'store_field' => new \Zend_Db_Expr('cs.store_id')
120+
'store_field' => new \Zend_Db_Expr('cs.store_id'),
120121
]
121122
);
122123

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
178178
'pid.attribute_id',
179179
'pid.store_id',
180180
'value' => $ifNullSql,
181+
'pid.entity_id',
181182
]
182183
)->where(
183184
'pid.attribute_id IN(?)',
@@ -200,7 +201,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
200201
'select' => $select,
201202
'entity_field' => new \Zend_Db_Expr('pid.entity_id'),
202203
'website_field' => new \Zend_Db_Expr('pid.website_id'),
203-
'store_field' => new \Zend_Db_Expr('pid.store_id')
204+
'store_field' => new \Zend_Db_Expr('pid.store_id'),
204205
]
205206
);
206207
$query = $select->insertFromSelect($idxTable);
@@ -221,11 +222,7 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
221222
$connection = $this->getConnection();
222223

223224
// prepare multiselect attributes
224-
if ($attributeId === null) {
225-
$attrIds = $this->_getIndexableAttributes(true);
226-
} else {
227-
$attrIds = [$attributeId];
228-
}
225+
$attrIds = $attributeId === null ? $this->_getIndexableAttributes(true) : [$attributeId];
229226

230227
if (!$attrIds) {
231228
return $this;
@@ -247,20 +244,20 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
247244
$productValueExpression = $connection->getCheckSql('pvs.value_id > 0', 'pvs.value', 'pvd.value');
248245
$select = $connection->select()->from(
249246
['pvd' => $this->getTable('catalog_product_entity_varchar')],
250-
[$productIdField, 'attribute_id']
247+
[]
251248
)->join(
252249
['cs' => $this->getTable('store')],
253250
'',
254-
['store_id']
251+
[]
255252
)->joinLeft(
256253
['pvs' => $this->getTable('catalog_product_entity_varchar')],
257254
"pvs.{$productIdField} = pvd.{$productIdField} AND pvs.attribute_id = pvd.attribute_id"
258255
. ' AND pvs.store_id=cs.store_id',
259-
['value' => $productValueExpression]
256+
[]
260257
)->joinLeft(
261258
['cpe' => $this->getTable('catalog_product_entity')],
262259
"cpe.{$productIdField} = pvd.{$productIdField}",
263-
['entity_id']
260+
['']
264261
)->where(
265262
'pvd.store_id=?',
266263
$connection->getIfNullSql('pvs.store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
@@ -272,6 +269,14 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
272269
$attrIds
273270
)->where(
274271
'cpe.entity_id IS NOT NULL'
272+
)->columns(
273+
[
274+
'entity_id' => 'cpe.entity_id',
275+
'attribute_id' => 'attribute_id',
276+
'store_id' => 'cs.store_id',
277+
'value' => $productValueExpression,
278+
'source_id' => 'cpe.entity_id',
279+
]
275280
);
276281

277282
$statusCond = $connection->quoteInto('=?', ProductStatus::STATUS_ENABLED);
@@ -289,30 +294,11 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
289294
'select' => $select,
290295
'entity_field' => new \Zend_Db_Expr('cpe.entity_id'),
291296
'website_field' => new \Zend_Db_Expr('cs.website_id'),
292-
'store_field' => new \Zend_Db_Expr('cs.store_id')
297+
'store_field' => new \Zend_Db_Expr('cs.store_id'),
293298
]
294299
);
295300

296-
$i = 0;
297-
$data = [];
298-
$query = $select->query();
299-
while ($row = $query->fetch()) {
300-
$values = explode(',', $row['value']);
301-
foreach ($values as $valueId) {
302-
if (isset($options[$row['attribute_id']][$valueId])) {
303-
$data[] = [$row['entity_id'], $row['attribute_id'], $row['store_id'], $valueId];
304-
$i++;
305-
if ($i % 10000 == 0) {
306-
$this->_saveIndexData($data);
307-
$data = [];
308-
}
309-
}
310-
}
311-
}
312-
313-
$this->_saveIndexData($data);
314-
unset($options);
315-
unset($data);
301+
$this->saveDataFromSelect($select, $options);
316302

317303
return $this;
318304
}
@@ -331,12 +317,43 @@ protected function _saveIndexData(array $data)
331317
$connection = $this->getConnection();
332318
$connection->insertArray(
333319
$this->getIdxTable(),
334-
['entity_id', 'attribute_id', 'store_id', 'value'],
320+
['entity_id', 'attribute_id', 'store_id', 'value', 'source_id'],
335321
$data
336322
);
323+
337324
return $this;
338325
}
339326

327+
/**
328+
* Prepares data from select to save.
329+
*
330+
* @param \Magento\Framework\DB\Select $select
331+
* @param array $options
332+
*
333+
* @return void
334+
*/
335+
private function saveDataFromSelect(\Magento\Framework\DB\Select $select, array $options)
336+
{
337+
$i = 0;
338+
$data = [];
339+
$query = $select->query();
340+
while ($row = $query->fetch()) {
341+
$values = explode(',', $row['value']);
342+
foreach ($values as $valueId) {
343+
if (isset($options[$row['attribute_id']][$valueId])) {
344+
$data[] = [$row['entity_id'], $row['attribute_id'], $row['store_id'], $valueId, $row['source_id']];
345+
$i++;
346+
if ($i % 10000 == 0) {
347+
$this->_saveIndexData($data);
348+
$data = [];
349+
}
350+
}
351+
}
352+
}
353+
354+
$this->_saveIndexData($data);
355+
}
356+
340357
/**
341358
* Retrieve temporary source index table name
342359
*

app/code/Magento/Catalog/Setup/InstallSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class InstallSchema implements InstallSchemaInterface
1818
/**
1919
* {@inheritdoc}
2020
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
21+
* @throws \Zend_Db_Exception
2122
*/
2223
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
2324
{
@@ -2426,7 +2427,6 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
24262427
'option_id',
24272428
$installer->getTable('catalog_product_option'),
24282429
'option_id',
2429-
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE,
24302430
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
24312431
)
24322432
->setComment(

0 commit comments

Comments
 (0)