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" />

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);
        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!

  • kindly use public function __construct() function and let me know if any issue
    – mohith
    Sep 20 '19 at 9:44
  • Hi @mohith! I have tried this but no success. Sep 20 '19 at 9:54

Add dependency to your etc/module.xml

            <module name="Magento_Catalog"/>

and clear cache and generated/code/

Happy coding

  • 1
    Hi lazy_coder! it is already implemented. Sep 20 '19 at 9:49

Add dependency to your etc/module.xml

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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