Skip to content

Commit b1bf893

Browse files
committed
MAGETWO-96118: Few optimizations on category & product pages
1 parent 5f976fa commit b1bf893

File tree

9 files changed

+127
-84
lines changed

9 files changed

+127
-84
lines changed

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public function __construct(
5555
*/
5656
public function execute($entity, $arguments = [])
5757
{
58-
$value = [];
59-
$value['images'] = [];
60-
6158
$mediaEntries = $this->resourceModel->loadProductGalleryByAttributeId(
6259
$entity,
6360
$this->getAttribute()->getAttributeId()
@@ -79,37 +76,13 @@ public function execute($entity, $arguments = [])
7976
*/
8077
public function addMediaDataToProduct(Product $product, array $mediaEntries)
8178
{
82-
$attrCode = $this->getAttribute()->getAttributeCode();
83-
$value = [];
84-
$value['images'] = [];
85-
$value['values'] = [];
86-
87-
foreach ($mediaEntries as $mediaEntry) {
88-
$mediaEntry = $this->substituteNullsWithDefaultValues($mediaEntry);
89-
$value['images'][$mediaEntry['value_id']] = $mediaEntry;
90-
}
91-
$product->setData($attrCode, $value);
92-
}
93-
94-
/**
95-
* @param array $rawData
96-
* @return array
97-
*/
98-
private function substituteNullsWithDefaultValues(array $rawData)
99-
{
100-
$processedData = [];
101-
foreach ($rawData as $key => $rawValue) {
102-
if (null !== $rawValue) {
103-
$processedValue = $rawValue;
104-
} elseif (isset($rawData[$key . '_default'])) {
105-
$processedValue = $rawData[$key . '_default'];
106-
} else {
107-
$processedValue = null;
108-
}
109-
$processedData[$key] = $processedValue;
110-
}
111-
112-
return $processedData;
79+
$product->setData(
80+
$this->getAttribute()->getAttributeCode(),
81+
[
82+
'images' => array_column($mediaEntries, null, 'value_id'),
83+
'values' => []
84+
]
85+
);
11386
}
11487

11588
/**

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function createBatchBaseSelect($storeId, $attributeId)
190190
'value.' . $linkField . ' = entity.' . $linkField,
191191
]
192192
),
193-
['label', 'position', 'disabled']
193+
[]
194194
)->joinLeft(
195195
['default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)],
196196
implode(
@@ -201,8 +201,15 @@ public function createBatchBaseSelect($storeId, $attributeId)
201201
'default_value.' . $linkField . ' = entity.' . $linkField,
202202
]
203203
),
204-
['label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled']
205-
)->where(
204+
[]
205+
)->columns([
206+
'label' => $this->getConnection()->getIfNullSql('`value`.`label`', '`default_value`.`label`'),
207+
'position' => $this->getConnection()->getIfNullSql('`value`.`position`', '`default_value`.`position`'),
208+
'disabled' => $this->getConnection()->getIfNullSql('`value`.`disabled`', '`default_value`.`disabled`'),
209+
'label_default' => 'default_value.label',
210+
'position_default' => 'default_value.position',
211+
'disabled_default' => 'default_value.disabled'
212+
])->where(
206213
$mainTableAlias . '.attribute_id = ?',
207214
$attributeId
208215
)->where(

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/GalleryTest.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ public function testBindValueToEntityRecordExists()
281281
$this->resource->bindValueToEntity($valueId, $entityId);
282282
}
283283

284+
/**
285+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
286+
*/
284287
public function testLoadGallery()
285288
{
286289
$productId = 5;
@@ -329,7 +332,8 @@ public function testLoadGallery()
329332
'main.value_id = entity.value_id',
330333
['entity_id']
331334
)->willReturnSelf();
332-
$this->product->expects($this->at(0))->method('getData')->with('entity_id')->willReturn($productId);
335+
$this->product->expects($this->at(0))->method('getData')
336+
->with('entity_id')->willReturn($productId);
333337
$this->product->expects($this->at(1))->method('getStoreId')->will($this->returnValue($storeId));
334338
$this->connection->expects($this->exactly(2))->method('quoteInto')->withConsecutive(
335339
['value.store_id = ?'],
@@ -338,26 +342,50 @@ public function testLoadGallery()
338342
'value.store_id = ' . $storeId,
339343
'default_value.store_id = ' . 0
340344
);
345+
$this->connection->expects($this->any())->method('getIfNullSql')->will(
346+
$this->returnValueMap([
347+
[
348+
'`value`.`label`',
349+
'`default_value`.`label`',
350+
'IFNULL(`value`.`label`, `default_value`.`label`)'
351+
],
352+
[
353+
'`value`.`position`',
354+
'`default_value`.`position`',
355+
'IFNULL(`value`.`position`, `default_value`.`position`)'
356+
],
357+
[
358+
'`value`.`disabled`',
359+
'`default_value`.`disabled`',
360+
'IFNULL(`value`.`disabled`, `default_value`.`disabled`)'
361+
]
362+
])
363+
);
341364
$this->select->expects($this->at(2))->method('joinLeft')->with(
342365
['value' => $getTableReturnValue],
343366
$quoteInfoReturnValue,
344-
[
345-
'label',
346-
'position',
347-
'disabled'
348-
]
367+
[]
349368
)->willReturnSelf();
350369
$this->select->expects($this->at(3))->method('joinLeft')->with(
351370
['default_value' => $getTableReturnValue],
352371
$quoteDefaultInfoReturnValue,
353-
['label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled']
372+
[]
354373
)->willReturnSelf();
355-
$this->select->expects($this->at(4))->method('where')->with(
374+
$this->select->expects($this->at(4))->method('columns')->with([
375+
'label' => 'IFNULL(`value`.`label`, `default_value`.`label`)',
376+
'position' => 'IFNULL(`value`.`position`, `default_value`.`position`)',
377+
'disabled' => 'IFNULL(`value`.`disabled`, `default_value`.`disabled`)',
378+
'label_default' => 'default_value.label',
379+
'position_default' => 'default_value.position',
380+
'disabled_default' => 'default_value.disabled'
381+
])->willReturnSelf();
382+
$this->select->expects($this->at(5))->method('where')->with(
356383
'main.attribute_id = ?',
357384
$attributeId
358385
)->willReturnSelf();
359-
$this->select->expects($this->at(5))->method('where')->with('main.disabled = 0')->willReturnSelf();
360-
$this->select->expects($this->at(7))->method('where')
386+
$this->select->expects($this->at(6))->method('where')
387+
->with('main.disabled = 0')->willReturnSelf();
388+
$this->select->expects($this->at(8))->method('where')
361389
->with('entity.entity_id = ?', $productId)
362390
->willReturnSelf();
363391
$this->select->expects($this->once())->method('order')

app/code/Magento/Catalog/etc/db_schema.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,11 @@
835835
<index referenceId="CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_VALUE_ID" indexType="btree">
836836
<column name="value_id"/>
837837
</index>
838+
<index referenceId="CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_ENTITY_ID_VALUE_ID_STORE_ID" indexType="btree">
839+
<column name="entity_id"/>
840+
<column name="value_id"/>
841+
<column name="store_id"/>
842+
</index>
838843
</table>
839844
<table name="catalog_product_option" resource="default" engine="innodb" comment="Catalog Product Option Table">
840845
<column xsi:type="int" name="option_id" padding="10" unsigned="true" nullable="false" identity="true"

app/code/Magento/Catalog/etc/db_schema_whitelist.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@
484484
"index": {
485485
"CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_STORE_ID": true,
486486
"CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_ENTITY_ID": true,
487-
"CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_VALUE_ID": true
487+
"CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_VALUE_ID": true,
488+
"CATALOG_PRODUCT_ENTITY_MEDIA_GALLERY_VALUE_ENTITY_ID_VALUE_ID_STORE_ID": true
488489
},
489490
"constraint": {
490491
"PRIMARY": true,
@@ -1121,4 +1122,4 @@
11211122
"CATALOG_PRODUCT_FRONTEND_ACTION_CUSTOMER_ID_PRODUCT_ID_TYPE_ID": true
11221123
}
11231124
}
1124-
}
1125+
}

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ public function getConfigurableAttributes($product)
453453
['group' => 'CONFIGURABLE', 'method' => __METHOD__]
454454
);
455455
if (!$product->hasData($this->_configurableAttributes)) {
456+
// for new product do not load configurable attributes
457+
if (!$product->getId()) {
458+
return [];
459+
}
456460
$configurableAttributes = $this->getConfigurableAttributeCollection($product);
457461
$this->extensionAttributesJoinProcessor->process($configurableAttributes);
458462
$configurableAttributes->orderByPosition()->load();
@@ -1398,23 +1402,47 @@ private function getConfiguredUsedProductCollection(
13981402
$skipStockFilter = true
13991403
) {
14001404
$collection = $this->getUsedProductCollection($product);
1405+
14011406
if ($skipStockFilter) {
14021407
$collection->setFlag('has_stock_status_filter', true);
14031408
}
1409+
14041410
$collection
1405-
->addAttributeToSelect($this->getCatalogConfig()->getProductAttributes())
1411+
->addAttributeToSelect($this->getAttributesForCollection($product))
14061412
->addFilterByRequiredOptions()
14071413
->setStoreId($product->getStoreId());
14081414

1409-
$requiredAttributes = ['name', 'price', 'weight', 'image', 'thumbnail', 'status', 'media_gallery'];
1410-
foreach ($requiredAttributes as $attributeCode) {
1411-
$collection->addAttributeToSelect($attributeCode);
1412-
}
1413-
foreach ($this->getUsedProductAttributes($product) as $usedProductAttribute) {
1414-
$collection->addAttributeToSelect($usedProductAttribute->getAttributeCode());
1415-
}
14161415
$collection->addMediaGalleryData();
14171416
$collection->addTierPriceData();
1417+
14181418
return $collection;
14191419
}
1420+
1421+
/**
1422+
* @return array
1423+
*/
1424+
private function getAttributesForCollection(\Magento\Catalog\Model\Product $product)
1425+
{
1426+
$productAttributes = $this->getCatalogConfig()->getProductAttributes();
1427+
1428+
$requiredAttributes = [
1429+
'name',
1430+
'price',
1431+
'weight',
1432+
'image',
1433+
'thumbnail',
1434+
'status',
1435+
'visibility',
1436+
'media_gallery'
1437+
];
1438+
1439+
$usedAttributes = array_map(
1440+
function($attr) {
1441+
return $attr->getAttributeCode();
1442+
},
1443+
$this->getUsedProductAttributes($product)
1444+
);
1445+
1446+
return array_unique(array_merge($productAttributes, $requiredAttributes, $usedAttributes));
1447+
}
14201448
}

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ public function getChildrenIds($parentId, $required = true)
173173
$parentId
174174
);
175175

176-
$childrenIds = [0 => []];
177-
foreach ($this->getConnection()->fetchAll($select) as $row) {
178-
$childrenIds[0][$row['product_id']] = $row['product_id'];
179-
}
176+
$childrenIds = [
177+
0 => array_column(
178+
$this->getConnection()->fetchAll($select),
179+
'product_id',
180+
'product_id'
181+
)
182+
];
180183

181184
return $childrenIds;
182185
}

app/code/Magento/ConfigurableProduct/Plugin/Catalog/Model/Product/Pricing/Renderer/SalableResolver.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public function afterIsSalable(
4444
\Magento\Framework\Pricing\SaleableInterface $salableItem
4545
) {
4646
if ($salableItem->getTypeId() == 'configurable' && $result) {
47-
if (!$this->lowestPriceOptionsProvider->getProducts($salableItem)) {
48-
$result = false;
49-
}
47+
$result = $salableItem->isSalable();
5048
}
5149

5250
return $result;

app/code/Magento/ProductVideo/Model/Plugin/ExternalVideoResourceBackend.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,34 @@ public function afterCreateBatchBaseSelect(Gallery $originalResourceModel, Selec
6060
'value.store_id = value_video.store_id',
6161
]
6262
),
63-
[
64-
'video_provider' => 'provider',
65-
'video_url' => 'url',
66-
'video_title' => 'title',
67-
'video_description' => 'description',
68-
'video_metadata' => 'metadata'
69-
]
63+
[]
7064
)->joinLeft(
71-
[
72-
'default_value_video' => $originalResourceModel->getTable(
73-
'catalog_product_entity_media_gallery_value_video'
74-
)
75-
],
65+
['default_value_video' => $originalResourceModel->getTable('catalog_product_entity_media_gallery_value_video')],
7666
implode(
7767
' AND ',
7868
[
7969
'default_value.value_id = default_value_video.value_id',
8070
'default_value.store_id = default_value_video.store_id',
8171
]
8272
),
83-
[
84-
'video_provider_default' => 'provider',
85-
'video_url_default' => 'url',
86-
'video_title_default' => 'title',
87-
'video_description_default' => 'description',
88-
'video_metadata_default' => 'metadata',
89-
]
90-
);
73+
[]
74+
)->columns([
75+
'video_provider' => $originalResourceModel->getConnection()
76+
->getIfNullSql('`value_video`.`provider`', '`default_value_video`.`provider`'),
77+
'video_url' => $originalResourceModel->getConnection()
78+
->getIfNullSql('`value_video`.`url`', '`default_value_video`.`url`'),
79+
'video_title' => $originalResourceModel->getConnection()
80+
->getIfNullSql('`value_video`.`title`', '`default_value_video`.`title`'),
81+
'video_description' => $originalResourceModel->getConnection()
82+
->getIfNullSql('`value_video`.`description`', '`default_value_video`.`description`'),
83+
'video_metadata' => $originalResourceModel->getConnection()
84+
->getIfNullSql('`value_video`.`metadata`', '`default_value_video`.`metadata`'),
85+
'video_provider_default' => 'default_value_video.provider',
86+
'video_url_default' => 'default_value_video.url',
87+
'video_title_default' => 'default_value_video.title',
88+
'video_description_default' => 'default_value_video.description',
89+
'video_metadata_default' => 'default_value_video.metadata',
90+
]);
9191

9292
return $select;
9393
}

0 commit comments

Comments
 (0)