2

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);
}
6
  • can you show some code?
    – Marius
    Commented Aug 23, 2016 at 8:20
  • @Marius i have updated my question
    – mp196
    Commented Aug 24, 2016 at 5:49
  • Since you add nothing new to the constructor of your class, what happens if you remove the __construct method in your class and delete the contents of the var/generation folder?
    – Marius
    Commented Aug 24, 2016 at 6:32
  • @Marius It seems i figured out the error, Actually when i try to instantiate the extended model layer.php object in my module block file either in construct or in the method using the object manager it used to throw that fatal error, but i am unaware of the exact issue still.
    – mp196
    Commented Aug 24, 2016 at 6:59
  • Did you find a solution?
    – Daniel
    Commented Apr 18, 2019 at 10:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.