1

Right now I am trying to override the default class of Magento 2 due to some my custom requirement but couldn't succeed, don't know why what I have done wrong here. May be someone can help me to the right direction.

For this I have written this code in my system,

<preference for="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection" type="COMPANY_NAME\MODULE_NAME\Model\ResourceModel\Fulltext\CatalogSearch\Collection"/> 

and I want to override this class,

Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection

EDIT

di.xml [app\code\COMPANY_NAME\MODULE_NAME\etc\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\CatalogSearch\Model\ResourceModel\Fulltext\Collection" type="COMPANY_NAME\MODULE_NAME\Model\ResourceModel\Fulltext\CatalogSearch\Collection"/>   
</config>

Thanks,

  • Have you tried to give permission for new added file "Collection.php" ? – Krupali Apr 4 '17 at 12:09
  • Yes @Krupali permission is correct :) – Keyur Shah Apr 4 '17 at 12:10
4

Finally I found the solution by my self, Actually It create an Issue because module CatalogSearchuse this class as virtualType in

vendor\magento\module-catalog-search\etc\di.xml

<virtualType name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\SearchCollection" type="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection">
    <arguments>
        <argument name="searchRequestName" xsi:type="string">quick_search_container</argument>
    </arguments>
</virtualType>

So we need to write virtualType class Instead of direct Magento search collection, like

<preference for="Magento\CatalogSearch\Model\ResourceModel\Fulltext\SearchCollection" type="YOUR_COMPANY_NAME\YOUR_MODULE_NAME\Model\ResourceModel\Fulltext\Collection" />
1

I test this code it is working for me file location etc/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\CatalogSearch\Model\ResourceModel\Fulltext\Collection" type="Spacename\Moduelname\Model\Rewrite\ResourceModel\Fulltext\Collection" />
</config>

Check if you missed something else.

  • Thanks for the answer, I tried the same thing that It is seems not working for me ? (: – Keyur Shah Apr 4 '17 at 12:41
  • @KeyurShah i think problem with you di.xml can you share the complete file also tell me is your module listed in admin? – Qaisar Satti Apr 4 '17 at 12:44
  • I am using this in frontend. And check my edits @Qaisar – Keyur Shah Apr 4 '17 at 12:49
  • @KeyurShah what is your class path it must be Accenture\Product\Model\ResourceModel\Fulltext\CatalogSearch\Collection.php also don't understand the frontend are you using etc/frontend/di.xml? – Qaisar Satti Apr 4 '17 at 12:52
  • Yes, just changed the path, still no luck,and I am using this file for frontend only. @Qaisar – Keyur Shah Apr 4 '17 at 13:08

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.