/** * {@inheritDoc} */ public function apply(\Magento\Framework\App\RequestInterface $request) { $categoryId = $request->getParam($this->_requestVar) ?: $request->getParam('id'); if (!empty($categoryId)) { $this->dataProvider->setCategoryId($categoryId); $category = $this->dataProvider->getCategory(); $this->applyCategoryFilterToCollection($category); if ($request->getParam('id') != $category->getId() && $this->dataProvider->isValid()) { $this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $categoryId)); } } return $this; }
/** * Apply category filter to layer * * @param \Magento\Framework\App\RequestInterface $request * @return $this */ public function apply(\Magento\Framework\App\RequestInterface $request) { $categoryId = (int) $request->getParam($this->getRequestVar()); if (!$categoryId) { return $this; } $this->dataProvider->setCategoryId($categoryId); if ($this->dataProvider->isValid()) { $category = $this->dataProvider->getCategory(); $this->getLayer()->getProductCollection()->addCategoryFilter($category); $this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $categoryId)); } return $this; }
public function testGetCategoryWithAppliedId() { $storeId = 1234; $categoryId = 4321; $this->store->expects($this->once())->method('getId')->will($this->returnValue($storeId)); $this->layer->expects($this->any())->method('getCurrentCategory')->will($this->returnValue($this->category)); $this->category->expects($this->once())->method('setStoreId')->with($this->equalTo($storeId))->will($this->returnSelf()); $this->category->expects($this->once())->method('load')->with($this->equalTo($categoryId))->will($this->returnSelf()); $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->category->expects($this->any())->method('getPathIds')->will($this->returnValue([20, 10])); $this->coreRegistry->expects($this->once())->method('register')->with($this->equalTo('current_category_filter'), $this->equalTo($this->category), $this->equalTo(true))->will($this->returnSelf()); $this->target->setCategoryId($categoryId); $this->assertSame($this->category, $this->target->getCategory()); $this->assertSame(20, $this->target->getResetValue()); return $this->target; }