/** * @param \Magento\Catalog\Model\Category $category * @param int $storeId * @return $this */ public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId) { /** @var $layer \Magento\Catalog\Model\Layer */ $layer = $this->catalogLayer->setStore($storeId); $collection = $category->getResourceCollection(); $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load(); /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $productCollection = $this->collectionFactory->create(); $currentCategory = $layer->setCurrentCategory($category); $layer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($collection); $category->getProductCollection()->setStoreId($storeId); $products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50); return $products; }
/** * @return string */ protected function _toHtml() { $categoryId = $this->getRequest()->getParam('cid'); $storeId = $this->_getStoreId(); /** @var $rssModel \Magento\Rss\Model\Rss */ $rssModel = $this->_rssFactory->create(); if ($categoryId) { $category = $this->_categoryFactory->create(); $category->load($categoryId); if ($category && $category->getId()) { /** @var $layer \Magento\Catalog\Model\Layer */ $layer = $this->_catalogLayer->setStore($storeId); //want to load all products no matter anchor or not $category->setIsAnchor(true); $newUrl = $category->getUrl(); $title = $category->getName(); $rssModel->_addHeader(array('title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8')); $_collection = $category->getCollection(); $_collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load(); /** @var $productCollection \Magento\Catalog\Model\Resource\Product\Collection */ $productCollection = $this->_collectionFactory->create(); $currentCategory = $layer->setCurrentCategory($category); $layer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($_collection); $category->getProductCollection()->setStoreId($storeId); /* only load latest 50 products */ $_productCollection = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->_visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50); if ($_productCollection->getSize() > 0) { $args = array('rssObj' => $rssModel); foreach ($_productCollection as $_product) { $args['product'] = $_product; $this->addNewItemXmlCallback($args); } } } } return $rssModel->createRssXml(); }