I am displaying a custom product collection in a block that extends the category block.
I have overriden catalog_category_view
in my custom module. The custom filter yields renders proper collection in the category page, but the layered navigation and the toolbar doesnot render the proper collection.
catalog_category_view.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Namespace\Module\Block\Category\Form" name="custom_category_collection" before=
"-" template="category/filter.phtml">
</block>
</referenceContainer>
</body>
</page>
app/code/namespace/module/Block/Category/Form.php
<?php
namespace Namespace\Module\Block\Category;
class Form extends \Magento\Framework\View\Element\Template
{
protected $_catalogLayer;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
array $data = []
) {
$this->_catalogLayer = $layerResolver->get();
parent::__construct($context, $data);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$collection = $this->_catalogLayer->getProductCollection();
$select = $collection->getSelect();
$select->where("e.entity_id = 18");
return $this;
}
}
Strange thing is, i noticed that the toolbar and layered navigation worked well with the custom collection when the module CatalogSearch
was disabled. But otherwise, it doesnt work.
Can anyone suggest a solution. Thanks.