magento2: Magento 2.4 catalog products sort by position is not working
Preconditions (*)
Magento v2.4.1
Steps to reproduce (*)
I’m trying to sort the products by position, but unfortunately it’s not working. I tried to debug it and printing out the request under the Elasticsearch7 Adapter
class, but I was getting an empty array for position sorting:
// vendor/magento/module-elasticsearch-7/SearchAdapter/Adapter.php:115:
array (size=0)
empty
If I’m selecting the price
or name
attribute as sorting option then I’m receiving the results correctly:
// vendor/magento/module-elasticsearch-7/SearchAdapter/Adapter.php:115:
array (size=1)
0 =>
array (size=1)
'price_0_1' =>
array (size=1)
'order' => string 'asc' (length=3)
It’s also working sorting by name:
array (size=1)
0 =>
array (size=1)
'name.sort_name' =>
array (size=1)
'order' => string 'asc' (length=3)
Can investigate the reason why it’s not working?
Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 75 (8 by maintainers)
nothing working.
Please try solution from here https://www.ecomplugins.com/blog/how-to-fix-magento-2-4-x-sort-by-position-category-products-issue/
This fixed for me 😃
vendor/magento/module-catalog/Block/Product/ProductList/toolbar.php at line 221 Remove the if its a position sort function, it works fine with the else function! So you get:
In our case we are running Magento 2.4.2 and Porto theme (latest version 4.0.0). Porto theme is coming with Mageplaza_Layered_Navigation + Mageplaza_Ajax_Layer extensions which interfere with the product sort on category pages.
We found the problem in: app/code/Mageplaza/LayeredNavigation/Model/ResourceModel/Fulltext/Collection.php The product collection gets sorted in the ElasticSearchApply()-function in the following way: $this->getSelect()->where(‘e.entity_id IN (?)’, $ids); $this->getSelect()->reset(Select::ORDER); $this->getSelect()->order(new Zend_Db_Expr(‘FIELD(e.entity_id,’ . implode(‘,’, $ids) . ‘)’));
So no matter what you set in the category page toolbar (position, name, price, ascending or descending) it always sorts the products by the entity_id attribute.
Its not enough if you disable Mageplaza_Layered_Navigation in the backend (I don’t know why right now).
The solution for us was to disable these extensions completely like this: bin/magento module:disable Mageplaza_LayeredNavigation --clear-static-content bin/magento module:disable Mageplaza_AjaxLayer --clear-static-content bin/magento setup:static-content:deploy bin/magento setup:di:compile
Now all the standard sort options work perfectly fine. Even though this is not a solution for the main problem some are facing I hope this maybe helps someone with a similar problem caused by this theme/extension 😃
@Jilco 2.4.4
@vipin12117
I have commented the file changes instead of removing them so you can see the changes.
vendor/magento/module-catalog/Block/Product/ProductList/toolbar.php at line 221
I am using Porto theme too and had encountered this problem on Magento 2.4.2. After above comment by @tabunsa, I looked up release notes for mageplaza’s layered navigation module and ajax module and realized that they had newer releases that were compatible with 2.4.2 and they fixed the bug. I used composer update mageplaza/* to update the the latest 4.1.1 module for layered navigation the issue is now resolved for anyone else who encountered this problem.
@MarkADurham in our case we are WeltPixel_GoogleTagManager and it is calling WeltPixel\GoogleTagManager\Block\Category.php and that create issue, but after upgrade it is fixed
So in your case may be some other module go to this file Magento\Elasticsearch7\SearchAdapter\Adapter.php
and debug which module called other than magento
@MarksAfanasevics i m faced similar issue and finally debug more than 1 day and found issue due to WeltPixel_GoogleTagManager
Hope it will help you