I am working on fetching layered navigation in magento2 for a custom product collection. I am getting custom collection already on the custom page need to show layered navigation. Tried to adapt this magento1 solution but couldn't get far.
Any idea how could i achieve it in magento2. What i have done so far is as below:
Extended the Catalog ListProduct block for custom product list on my custom page.
class View extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
array $data = [],
\Custom\LayerNavigation\Model\Layer $testlayerobj
) {
parent::__construct($context,$postDataHelper,$layerResolver,
$categoryRepository,$urlHelper,$data);
$this->_coreRegistry = $context->getRegistry();
$this->_testlayer = $testlayerobj;
}
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
$this->_productCollection = $this->getLayer()->getProductCollection();
}
return $this->_productCollection;
}
public function getLayer()
{
$layer = $this->_coreRegistry->registry('current_layer');
if ($layer) {
return $layer;
}
return $this->_testlayer;
}
}
Used the core Search block for layered navigation in layout file
<referenceContainer name="sidebar.main">
<block class="Magento\LayeredNavigation\Block\Navigation\Search" name="catalogsearch.leftnav" before="-" template="layer/view.phtml">
<block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalogsearch.navigation.state" as="state" />
<block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalogsearch.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
</block>
</referenceContainer>
Extended Core layer model to modify the collection.
class Layer extends \Magento\Catalog\Model\Layer
{
public function __construct(
optionStoreFactory $optionstoreFactory,
Attrhelper $attrhelper,
productCollectionFactory $productCollectionFactory,
AttributevalueFactory $attributevalueFactory,
CategoryRepositoryInterface $categoryRepository,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Model\Layer\Search\CollectionFilter $filter,
array $data = []
) {
$this->optionstoreFactory = $optionstoreFactory;
$this->attributevalueFactory = $attributevalueFactory;
$this->_attrhelper = $attrhelper;
$this->request = $request;
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryRepository = $categoryRepository;
$this->_storeManager = $storeManager;
$this->filter = $filter;
$this->registry = $registry;
}
public function getProductCollection()
{
$attributevalue = $this->getAttributeValue(); //eg: Manufacturer Attribute details
$attr_code = $attributevalue->getAttributeCode();
$attr_option_value = $attributevalue->getOptionId();
$collection = $this->productCollectionFactory->create();
$store_id = $this->request->getParam('store_id');
$collection->addAttributeToSelect('*')
->addAttributeToFilter($attr_code , ['finset'=>$attr_option_value ])
->addAttributeToFilter('visibility','4')
->setStore($store_id)
->addFieldToFilter('status', array('eq' => 1))
->setOrder('id', 'DESC');
$this->prepareProductCollection($collection);
return $collection;
}
public function prepareProductCollection($collection)
{
$this->filter->filter($collection, $this->getCurrentCategory());
return $this;
}
public function getCurrentCategory()
{
$category = $this->registry->registry('current_category');
if ($category) {
$this->setData('current_category', $category);
} else {
$category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId());
$this->setData('current_category', $category);
}
return $category;
}
public function getCurrentStore()
{
return $this->_storeManager->getStore();
}
}
As of now i am getting the layer navigation displayed but it ain't specific to my custom collection. As per my debugging the collection is filtered all the way from root category (Which contains whole product catalog) and according to it is fetching the layers.