2

I've created a plugin in Magento 2.4.5 to overwrite Magento\CatalogSearch\Model\Layer\Filter\Price '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="Magento\CatalogSearch\Model\Layer\Filter\Price">
        <plugin name="magento_core_price" type="MyVendor\MyModule\Model\Magento\Price" sortOrder="1" />
    </type>

</config>

After that I'm getting following error:

Cannot instantiate interface Magento\Catalog\Model\Layer\ContextInterface

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 MyVendor\MyModule\Model\Magento\Price

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

namespace MyVendor\MyModule\Model\Layer\Filter;

use Magento\Catalog\Model\Layer\Filter\AbstractFilter;

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


    /**
     * Apply price range filter
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return $this
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function afterapply(\Magento\Framework\App\RequestInterface $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;
    }

}

edit 2

after trying @Msquare answer still the same error

5
  • Can you share the file path and also provide your MyVendor\MyModule\Model\Magento\Price mentioned file here so we can understand better?
    – Msquare
    Dec 1, 2023 at 15:03
  • @Msquare don't really understand what do yo mean with file path. Also updated the question with MyVendor\MyModule\Model\Magento\Price contents.
    – Yaroslav
    Dec 1, 2023 at 16:13
  • try to extends Magento\CatalogSearch\Model\Layer\Filter\Price instead of AbstractFilter Dec 1, 2023 at 19:43
  • @Yaroslav please check this link : magento.stackexchange.com/a/371066/82670
    – Msquare
    Dec 3, 2023 at 7:40
  • @Yaroslav Please contact Stack Exchange to merge your accounts: magento.stackexchange.com/contact Dec 4, 2023 at 15:44

1 Answer 1

1

Please try the below code.

app/code/VendorName/ModuleName/etc

di.xml

<type name="Magento\CatalogSearch\Model\Layer\Filter\Price">
       <plugin name="magento_core_price_plugin" sortOrder="1"  type="VendorName\ModuleName\Plugin\Model\Layer\Filter\Price"/>
</type>

app/code/VendorName/ModuleName/Plugin/Model/Layer/Filter

Price.php

<?php

namespace VendorName\ModuleName\Plugin\Model\Layer\Filter;

use Magento\Catalog\Model\Layer\Filter\AbstractFilter;

class Price
{
    public function afterApply(
        \Magento\CatalogSearch\Model\Layer\Filter\Price $subject,
        $result,
        \Magento\Framework\App\RequestInterface $request
    ) {

        echo " Your Module Plugin Called";
        exit();
        // Add Your Logic Here
    }

}

Your di.xml file is correct, but your plugin file contains errors. You made two mistakes. First, you wrote the wrong method name, and second, you added the wrong parameter to the plugin method. You can compare it with the above working code for reference.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.