Skip to content

Error: Cannot instantiate interface MagentoCatalogModelLayerContextInterface Magento Plugin

I’ve created a plugin in Magento 2.4.5 to overwrite MagentoCatalogSearchModelLayerFilterPrice ‘s method.

<?xml version="1.0"?>


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="MagentoCatalogSearchModelLayerFilterPrice">
        <plugin name="magento_core_price" type="MyVendorMyModuleModelMagentoPrice" sortOrder="1" />
    </type>

</config>

After that I’m getting following error:

Cannot instantiate interface MagentoCatalogModelLayerContextInterface

I’ve already tried to clear cache, to upgrade, to setup:di:compile , to remove the var/generation folder but nothing worked for me. Any idea what can it be?

Edit:

My MyVendorMyModuleModelMagentoPrice

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace MyVendorMyModuleModelLayerFilter;

use MagentoCatalogModelLayerFilterAbstractFilter;

/**
 * Layer price filter based on Search API
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
 */
class Price extends AbstractFilter
{


    /**
     * Apply price range filter
     *
     * @param MagentoFrameworkAppRequestInterface $request
     * @return $this
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function afterapply(MagentoFrameworkAppRequestInterface $request)
    {
        /**
         * Filter must be string: $fromPrice-$toPrice
         */
        $filter = $request->getParam($this->getRequestVar());
        if (!$filter || is_array($filter)) {
            return $this;
        }

        $filterParams = explode(',', $filter);
        $filter = $this->dataProvider->validateFilter($filterParams[0]);
        if (!$filter) {
            return $this;
        }

        $this->dataProvider->setInterval($filter);
        $priorFilters = $this->dataProvider->getPriorFilters($filterParams);
        if ($priorFilters) {
            $this->dataProvider->setPriorIntervals($priorFilters);
        }

        list($from, $to) = $filter;
        $this->getLayer()->getProductCollection()->getSize();
        $this->getLayer()->getProductCollection()->addAttributeToFilter(
            'price',
            ['from' => $from, 'to' =>  $to ]
        );

        $this->getLayer()->getState()->addFilter(
            $this->_createItem($this->_renderRangeLabel(empty($from) ? 0 : $from, $to), $filter)
        );

        return $this;
    }

}