2

It seems like Overriding Block and Model in magento2 work only if the class is directly being called.

For Example i want to override model file

vendor/magento/module-catalog/Model/Layer/Filter/AbstractFilter.php

Here is my di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <preference for="Magento\Catalog\Model\Layer\Filter\AbstractFilter" type="Mynamespace\Comman\Model\Layer\Filter\AbstractFilter" />
</config>

And This is my AbstractFilter.php

namespace Mynamespace\Comman\Model\Layer\Filter;

abstract class AbstractFilter extends \Magento\Catalog\Model\Layer\Filter\AbstractFilter
{      
    public function __construct(
        \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Layer $layer,
        \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
        array $data = []
    ) {

        parent::__construct($filterItemFactory,$storeManager,$layer,$itemDataBuilder,$data);
        if ($this->hasAttributeModel()) {
            $this->_requestVar = $this->getAttributeModel()->getAttributeCode();
        }
    }

    /* i want to override this function*/
    protected function isOptionReducesResults($optionCount, $totalSize)
    {
        return $optionCount <= $totalSize;
    }
}

This is not working because AbstractFilter is not being called directly from anywhere.

If i do same thing for

vendor/magento/module-catalog/Model/Layer/Filter/Price.php

It is working fine.

I am facing same issue for class:

vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle/Option.php

Which is not working however classes

vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle/Option/Checkbox.php

vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle/Option/Radio.php

which are working fine.

Can someone guide me if i am wrong here.

2
  • magento.stackexchange.com/questions/157797/… give u some hint if u want to override abstract class
    – Jackson
    Commented Feb 6, 2017 at 10:19
  • @AnkitShah This is not the case for only abstract class.vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle/Option.php is not abstract class but is parent of Checkbox and Radio Commented Feb 6, 2017 at 10:53

0

Your Answer

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