Skip to content

Commit 985c679

Browse files
authored
Merge pull request #5572 from magento-performance/MC-33086-catalog-event
[performance] MC-33086: [Improvement] Apply Event To Category observer
2 parents c4a7d77 + 6ee27c1 commit 985c679

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ public function getLayer()
136136
*/
137137
public function getLoadedProductCollection()
138138
{
139-
return $this->_getProductCollection();
139+
$collection = $this->_getProductCollection();
140+
141+
$categoryId = $this->getLayer()->getCurrentCategory()->getId();
142+
foreach ($collection as $product) {
143+
$product->setData('category_id', $categoryId);
144+
}
145+
146+
return $collection;
140147
}
141148

142149
/**

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,14 @@ public function getIdBySku($sku)
725725
*/
726726
public function getCategoryId()
727727
{
728+
if ($this->hasData('category_id')) {
729+
return $this->getData('category_id');
730+
}
728731
$category = $this->_registry->registry('current_category');
729-
if ($category && in_array($category->getId(), $this->getCategoryIds())) {
730-
return $category->getId();
732+
$categoryId = $category ? $category->getId() : null;
733+
if ($categoryId && in_array($categoryId, $this->getCategoryIds())) {
734+
$this->setData('category_id', $categoryId);
735+
return $categoryId;
731736
}
732737
return false;
733738
}

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ public function testGetAdditionalHtml()
9292

9393
public function testSetCollection()
9494
{
95-
$this->_block->setCollection('test');
96-
$this->assertEquals('test', $this->_block->getLoadedProductCollection());
95+
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
96+
->create(\Magento\Framework\Data\Collection::class);
97+
$this->_block->setCollection($collection);
98+
$this->assertEquals($collection, $this->_block->getLoadedProductCollection());
9799
}
98100

99101
public function testGetPriceBlockTemplate()

0 commit comments

Comments
 (0)