Skip to content

Commit baebe46

Browse files
committed
MC-33086: [Improvement] Apply Event To Category observer
1 parent 15dc874 commit baebe46

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
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
}

0 commit comments

Comments
 (0)