I need to create a page that automatically shows a list of all products with catalog rules applied. This page should also include layered navigation.
Which is te best way to achieve it?
I tried creating a page and link it to a block:
{{block class="Vendor\ModuleName\Block\Sales" template="Vendor_ModuleName::sales.phtml"}}
And appended layered navigation in Layout Update XML area, linking Mageplaza_LayeredNavigation overriding Magento Layered navigation.
Then, in my module I created the sales.phtml page inside /view/frontend/templates with the view on it and the block/Sales.php, with a custom product collection.
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
$layer = $this->getLayer();
$this->_productCollection = $layer->getProductCollection();
$params = $this->getRequest()->getParams();
if(count($params)){
// Make sure array key is attribute code
foreach($params as $key => $value) {
if ($key != "page_id") {
$this->_productCollection->addAttributeToFilter($key,array('eq' => intval($value)));
}
}
}
$this->_productCollection->getSize();
}
return $this->_productCollection;
}
The thing is that I'm able to show products correctly but when I filter I'm facing several problems:
- It appends a search_tmp_xxx table that makes no result returned
- It makes that layered navigation appends the attribute and value to url when filter applied but not refreshing data until you manually refresh the page
- The attribute count in layered navigation does not count items correctly