Been Working on Layered Navigation for my custom collection which is displayed on a custom page, Have extended the Magento\Catalog\Model\Layer
in my custom class and have added the preference in di.xml
for the same <preference for="Magento\Catalog\Model\Layer" type="Custom\LayeredNav\Model\Layer" />
i need to override just one method i.e prepareProductCollection()
so i could replace the default collection with the custom collection.
After Following all these steps I have been stucked on the error as below:
Fatal error: Uncaught Error: Cannot instantiate interface Magento\Catalog\Model\Layer\ContextInterface
Any guidance would be really helpful. Below is the code.
Di.xml
<preference for="Magento\Catalog\Model\Layer" type="Custom\LayeredNav\Model\Layer" />
Custom Layer File
<?php
namespace Custom\LayeredNav\Model;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Catalog\Model\Layer\ContextInterface;
class Layer extends \Magento\Catalog\Model\Layer
{
public function __construct(
ContextInterface $context,
\Magento\Catalog\Model\Layer\StateFactory $layerStateFactory,
AttributeCollectionFactory $attributeCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product $catalogProduct,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Registry $registry,
CategoryRepositoryInterface $categoryRepository,
array $data = []
) {
parent::__construct(
$context,
$layerStateFactory,
$attributeCollectionFactory,
$catalogProduct,
$storeManager,
$registry,
$categoryRepository,
$data
);
}
public function prepareProductCollection($collection)
{
parent::prepareProductCollection($collection);
//custom collection
$custom_collection = $this->registry->registry('md_optioncollection');
//Have set root category id as current category as such the filtered collection is not related to specific category
$curr_cat_id = $this->registry->registry('current_category')->getId();
$this->collectionFilter->filter($custom_collection, $curr_cat_id);
return $this;
}
}
Custom collection set in my controller file which i store in registry.
//this is where i set my custom collection in another file
public function getAttributeCollection()
{
$attributevalue = $this->getAttributeValue();
$attr_code = $attributevalue->getAttributeCode();
$attr_option_value = $attributevalue->getOptionId();
$collection = $this->productCollectionFactory->create();
$store_id = $this->getRequest()->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->setCollection($collection);
$this->_coreRegistry->register('md_optioncollection', $collection);
}
__construct
method in your class and delete the contents of thevar/generation
folder?