Skip to content

Commit

Permalink
Merge pull request #5572 from magento-performance/MC-33086-catalog-event
Browse files Browse the repository at this point in the history
[performance] MC-33086: [Improvement] Apply Event To Category observer
  • Loading branch information
vzabaznov committed Apr 14, 2020
2 parents c4a7d77 + 6ee27c1 commit 985c679
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 8 additions & 1 deletion app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ public function getLayer()
*/
public function getLoadedProductCollection()
{
return $this->_getProductCollection();
$collection = $this->_getProductCollection();

$categoryId = $this->getLayer()->getCurrentCategory()->getId();
foreach ($collection as $product) {
$product->setData('category_id', $categoryId);
}

return $collection;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,14 @@ public function getIdBySku($sku)
*/
public function getCategoryId()
{
if ($this->hasData('category_id')) {
return $this->getData('category_id');
}
$category = $this->_registry->registry('current_category');
if ($category && in_array($category->getId(), $this->getCategoryIds())) {
return $category->getId();
$categoryId = $category ? $category->getId() : null;
if ($categoryId && in_array($categoryId, $this->getCategoryIds())) {
$this->setData('category_id', $categoryId);
return $categoryId;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public function testGetAdditionalHtml()

public function testSetCollection()
{
$this->_block->setCollection('test');
$this->assertEquals('test', $this->_block->getLoadedProductCollection());
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Framework\Data\Collection::class);
$this->_block->setCollection($collection);
$this->assertEquals($collection, $this->_block->getLoadedProductCollection());
}

public function testGetPriceBlockTemplate()
Expand Down

0 comments on commit 985c679

Please sign in to comment.