2

I have this issue with my Magento 2.4.3, which does not sort by position.

I found this code where I need to override, but I am quite new to Magento. Is there anyone that can show me or tell me how to override: Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection

The code:

    protected function _renderFiltersBefore()
    {
        if(isset($_SERVER['REQUEST_URI']) && !stristr($_SERVER['REQUEST_URI'], "catalogsearch")){
             $request = ObjectManager::getInstance()->create("\Magento\Framework\App\RequestInterface");
             if($request->getParam("product_list_order")){
                  $attribute = $request->getParam("product_list_order", "position");
                  $direction = $request->getParam("product_list_dir", "asc");
                  $this->addAttributeToSort($attribute, $direction);
             }
             else{
                $this->addAttributeToSort("position", "asc");
             }
        }

As far as I can understand, this should just be added and not replaced with anything.

David Lambauer
  • 1,071
  • 2
  • 9
  • 25

1 Answers1

1

Your code found in the issue that was reported to Magento github: https://github.com/magento/magento2/issues/34502

Firstly, I'm not sure that code resolves the sorting product by position issue.

Now, let's back to your question: how to override Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection to fix the sort by position issue on the category page?

Override Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection is quite complex and different from other classes. Due to the issue only affected on the category page we have to override elasticsearchCategoryCollection which override Magento\CatalogSearch\Model\ResourceModel\Fulltext\CollectionFactory

To do this, take the following steps:

Step 1: Create registration.php:

  • File path: app/code/Jan/CatalogSearch/registration.php
  • Content:
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Jan_CatalogSearch', DIR);

Step 2: Create module.xml:

  • File path: app/code/Jan/CatalogSearch/etc/module.xml
  • Content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jan_CatalogSearch">
        <sequence>
            <module name="Magento_CatalogSearch"/>
            <module name="Magento_Elasticsearch"/>
        </sequence>
    </module>
</config>

Step 3: Create di.xml:

  • File path: app/code/Jan/CatalogSearch/etc/di.xml
  • Content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="elasticsearchCategoryCollection" type="Jan\CatalogSearch\Model\ResourceModel\Fulltext\Collection"/>
</config>

Step 4: Create Model\ResourceModel\Fulltext\Collection file.

  • File path: app/code/Jan/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php
  • Content:
<?php

namespace Jan\CatalogSearch\Model\ResourceModel\Fulltext;

use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\DefaultFilterStrategyApplyCheckerInterface; use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\TotalRecordsResolverFactory; use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolverFactory; use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierFactory; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Api\Search\SearchResultFactory; use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;

/**

  • Fulltext Collection
  • Customize to fix sort by position does not work in magento 2.4
  • This collection should be refactored to not have dependencies on MySQL-specific implementation.
  • @api
  • @since 100.0.2
  • @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  • @SuppressWarnings(PHPMD.TooManyFields)
  • @SuppressWarnings(PHPMD.CookieAndSessionMisuse)

/ class Collection extends \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection { /* * @var \Magento\Framework\App\RequestInterface */ private $request;

/**
 * @var string
 */
private string $searchRequestName;

/**
 * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
 * @param \Psr\Log\LoggerInterface $logger
 * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
 * @param \Magento\Framework\Event\ManagerInterface $eventManager
 * @param \Magento\Eav\Model\Config $eavConfig
 * @param \Magento\Framework\App\ResourceConnection $resource
 * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
 * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
 * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Framework\Module\Manager $moduleManager
 * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
 * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
 * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Framework\Stdlib\DateTime $dateTime
 * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
 * @param \Magento\Framework\App\RequestInterface $request
 * @param mixed $catalogSearchData
 * @param mixed $requestBuilder
 * @param mixed $searchEngine
 * @param mixed $temporaryStorageFactory
 * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
 * @param string $searchRequestName
 * @param SearchResultFactory|null $searchResultFactory
 * @param ProductLimitationFactory|null $productLimitationFactory
 * @param MetadataPool|null $metadataPool
 * @param \Magento\Search\Api\SearchInterface|null $search
 * @param \Magento\Framework\Api\Search\SearchCriteriaBuilder|null $searchCriteriaBuilder
 * @param \Magento\Framework\Api\FilterBuilder|null $filterBuilder
 * @param SearchCriteriaResolverFactory|null $searchCriteriaResolverFactory
 * @param SearchResultApplierFactory|null $searchResultApplierFactory
 * @param TotalRecordsResolverFactory|null $totalRecordsResolverFactory
 * @param DefaultFilterStrategyApplyCheckerInterface|null $defaultFilterStrategyApplyChecker
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function __construct(
    \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Eav\Model\Config $eavConfig,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Eav\Model\EntityFactory $eavEntityFactory,
    \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
    \Magento\Framework\Validator\UniversalFactory $universalFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Module\Manager $moduleManager,
    \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
    \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\Stdlib\DateTime $dateTime,
    \Magento\Customer\Api\GroupManagementInterface $groupManagement,
    \Magento\Framework\App\RequestInterface $request,
    $catalogSearchData = null,
    $requestBuilder = null,
    $searchEngine = null,
    $temporaryStorageFactory = null,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    $searchRequestName = 'catalog_view_container',
    SearchResultFactory $searchResultFactory = null,
    ProductLimitationFactory $productLimitationFactory = null,
    MetadataPool $metadataPool = null,
    \Magento\Search\Api\SearchInterface $search = null,
    \Magento\Framework\Api\Search\SearchCriteriaBuilder $searchCriteriaBuilder = null,
    \Magento\Framework\Api\FilterBuilder $filterBuilder = null,
    SearchCriteriaResolverFactory $searchCriteriaResolverFactory = null,
    SearchResultApplierFactory $searchResultApplierFactory = null,
    TotalRecordsResolverFactory $totalRecordsResolverFactory = null,
    DefaultFilterStrategyApplyCheckerInterface $defaultFilterStrategyApplyChecker = null
) {
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $eavConfig,
        $resource,
        $eavEntityFactory,
        $resourceHelper,
        $universalFactory,
        $storeManager,
        $moduleManager,
        $catalogProductFlatState,
        $scopeConfig,
        $productOptionFactory,
        $catalogUrl,
        $localeDate,
        $customerSession,
        $dateTime,
        $groupManagement,
        $catalogSearchData,
        $requestBuilder,
        $searchEngine,
        $temporaryStorageFactory,
        $connection,
        $searchRequestName,
        $searchResultFactory,
        $productLimitationFactory,
        $metadataPool,
        $search,
        $searchCriteriaBuilder,
        $filterBuilder,
        $searchCriteriaResolverFactory,
        $searchResultApplierFactory,
        $totalRecordsResolverFactory,
        $defaultFilterStrategyApplyChecker
    );
    $this-&gt;request = $request;
    $this-&gt;searchRequestName = $searchRequestName;
}

/**
 * @inheritdoc
 */
protected function _renderFiltersBefore()
{
    if ($this-&gt;searchRequestName === 'catalog_view_container') {
        $attribute = $this-&gt;request-&gt;getParam(&quot;product_list_order&quot;, &quot;position&quot;);
        $direction = $this-&gt;request-&gt;getParam(&quot;product_list_dir&quot;, &quot;asc&quot;);
        $this-&gt;addAttributeToSort($attribute, $direction);
    }

    parent::_renderFiltersBefore();
}

}

Step 5: Run setup upgrade command to install and enable the module: bin/magento setup:upgrade

Step 6: Run compilation code command: bin/magento setup:di:compile

Step 7: Run deploy static content command: bin/magento setup:static-content:deploy -f

You are done.

====

Explaination more about the change to your code: You code has some issues:

Tu Van
  • 6,868
  • 2
  • 11
  • 22
  • Thank you for your detailed answer, then I need to ask, can this be an issue with our 3rd party ES module as you are mentioning elasticsearchCategoryCollection ? – Jan-petter Havna Oct 18 '22 at 14:01
  • No, elasticsearchCategoryCollection get from Magento_Elasticsearch which I added into sequence in module.xml – Tu Van Oct 18 '22 at 14:04
  • The code you have posted here? does that include the "fix"? or do I need to add them to this? Not sure if I understand you 100% as you said the code I pasted might not work, is your code better or do i need to add it to what you have posted? – Jan-petter Havna Oct 25 '22 at 13:57
  • I'm not sure... I've said that in the answer: I'm not sure if the code you get from the github issue resolves the sorting by position or not because I've tested it in Magento 2.4.5 (that code does not resolve the issue) but you use 2.4.3. And then I answer your question about how to override that collection class. – Tu Van Oct 25 '22 at 15:05
  • You can quickly test if that code (get from the github issue) work as expected or not by adding the content of _renderFiltersBefore() method in your question to vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php line 453, before if ($this->isLoaded()) {, then remove generated folder and re compile code. JUST DO IT in your locally, don't do it in production. – Tu Van Oct 25 '22 at 15:10
  • I can confirm that you were right, it did not work. Do you have an idea how to fix this issue? – Jan-petter Havna Oct 31 '22 at 20:12
  • I didn't dig deep into this issue, maybe I can spend time investigating it this week. Anyway, follow the github issue: https://github.com/magento/magento2/issues/34502 – Tu Van Nov 01 '22 at 01:15