diff --git a/app/code/Magento/Catalog/Block/Product/ListProduct.php b/app/code/Magento/Catalog/Block/Product/ListProduct.php index 59f5cc1b53b26..76fcdfbf232e5 100644 --- a/app/code/Magento/Catalog/Block/Product/ListProduct.php +++ b/app/code/Magento/Catalog/Block/Product/ListProduct.php @@ -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; } /** diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index a9907c1661bd8..fd59130db4bdf 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -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; } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php index 6c735e52f8c1a..25e26bc66ab51 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php @@ -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()