2

When filters + Ancor is enabled for category, sorting by product name doesn't work. I know the fix as I tested in Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection. So now I'm trying to override it in my module but below does not work. It still reads from core file.

<?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="MyVendor\Mymodule\Model\ResourceModel\Fulltext\Collection" />
</config>

Below did not work too:

 <virtualType name="MyVendor\MyModule\Model\ResourceModel\Fulltext\CollectionFactory" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
            <arguments>
                <argument name="instanceName" xsi:type="string">MyVendor\MyModule\Model\ResourceModel\Fulltext\Collection</argument>
            </arguments>
 </virtualType>
6
  • Where you put this code @Zinat .? I means in etc/di.xml or etc/frontend/di.xml.
    – user53816
    Commented Mar 13, 2018 at 6:19
  • In etc/di.xml ... There is some trick about virtualType as I read now
    – Zinat
    Commented Mar 13, 2018 at 6:31
  • Which Magento2 version? Commented Mar 13, 2018 at 6:45
  • the version is EE 2.2.2
    – Zinat
    Commented Mar 13, 2018 at 6:46
  • Did you check after deleting generator directory?
    – Shireen N
    Commented Mar 13, 2018 at 7:12

4 Answers 4

3

It work for me. I override the Catalog\Model\ResourceModel\fulltext\Collection.php. Create a etc\di.xml file. write the code

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

create Collection.php file.

<?php
namespace Vendor\Catalog\Model\ResourceModel\Fulltext;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{
}
3

The reason the override for Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection doesn't work is because of the virtual type override in vendor/magento/module-elasticsearch/etc/di.xml. In your di.xml you need to declare the following preferences:

<preference for="elasticsearchFulltextSearchCollection" type="Vendor\Module\Model\ResourceModel\Fulltext\Collection" />
    <preference for="elasticsearchCategoryCollection" type="Vendor\Module\Model\ResourceModel\Fulltext\Collection" />
    <preference for="elasticsearchAdvancedCollection" type="Vendor\Module\Model\ResourceModel\Fulltext\Collection" />
2

Try following way:


<virtualType name="VendorName\ModuleName\Model\ResourceModel\Fulltext\CollectionFactory" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">VendorName\ModuleName\Model\ResourceModel\Fulltext\Collection</argument>
    </arguments>
</virtualType>
<virtualType name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\SearchCollection" type="VendorName\ModuleName\Model\ResourceModel\Fulltext\Collection">
    <arguments>
        <argument name="searchRequestName" xsi:type="string">quick_search_container</argument>
    </arguments>
</virtualType>

VendorName/ModuleName/Model/ResourceModel/Fulltext/Collection.php


<?php
namespace VendorName\ModuleName\Model\ResourceModel\Fulltext;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{
}
2
  • I tried this but It not works. Can your give me an example for it? Commented Jun 22, 2018 at 8:03
  • @SohelRana I added same things. But, It's not return collection. I set ElasticSearch 5.0+ version of magento default. Can you please help me? What should I need to do? Commented Jan 20, 2020 at 11:31
1

The reason why it doesn’t work is because the class

Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection

is used in the CatalogSearch with customization through virtual types and there is no direct usage of this class. Customization of the source class for a virtual type is not possible because the ObjectManager resolves preference before resolving is a virtual type used and if that, then which class it should instantiate. However, with the current implementation, you can customize specific virtual types and it should help you.

In Magento latest version I can find only one virtual type for the class

Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection :

<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 to resolve your problem need define preference for the virtual type:

<preference for="Magento\CatalogSearch\Model\ResourceModel\Fulltext\SearchCollection" type="CompanyName\ModuleName\Model\ResourceModel\Fulltext\Collection" />

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.