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.