0

I want to override Magento\Catalog\Model\ResourceModel\Product\Collection using preference to modify the _buildClearSelect($select = null) method.

For this, I have created di.xml with the following code:

<?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\ResourceModel\Product\Collection" type="Vendor\ModuleName\Model\ResourceModel\Product\Collection" />
</config>

and Collection.php

namespace Vendor\ModuleName\Model\ResourceModel\Product;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{
    /**
     * Build clear select
     *
     * @param \Magento\Framework\DB\Select $select
     * @return \Magento\Framework\DB\Select
     */
    protected function _buildClearSelect($select = null)
    {
        $select = parent::_buildClearSelect($select);
        $select->reset(\Magento\Framework\DB\Select::GROUP);
        return $select;
    }
}

Above code is not working. I have already tried to flush cache and remove the code directory from the generated directory.

Any help would be appreciated!

3
  • kindly use public function __construct() function and let me know if any issue
    – mohith
    Commented Sep 20, 2019 at 9:44
  • Hi @mohith! I have tried this but no success. Commented Sep 20, 2019 at 9:54
  • hey @SanchitGupta did you figure it out? I'm facing similar issue with some other Protected Method Override
    – Asad Khan
    Commented Feb 11 at 18:29

2 Answers 2

0

Add dependency to your etc/module.xml

        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>

and clear cache and generated/code/

Happy coding

1
  • 1
    Hi lazy_coder! it is already implemented. Commented Sep 20, 2019 at 9:49
0

Add dependency to your etc/module.xml

   <sequence>
        <module name="Magento_Backend" />
    </sequence>
3
  • Hello Mohith! Can you please explain why we add Magento_Backend? Commented Sep 20, 2019 at 10:44
  • Its working right!!!
    – mohith
    Commented Sep 20, 2019 at 10:47
  • No, it's not working. Commented Sep 20, 2019 at 10:49

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.