last day (15 days later) » 

10:58 AM
0
Q: Magento 2 - Unable to change category products position

FbrIn Magento 2.3.0, I try to change position for category products. In admin, each position is set but in the frontend, nothing changes. Even after clear/flush cache and reindexing. Do you know how and in which file makes this change programmatically to get product collection sort by product posi...

 
Did you also check in the admin if the category default sort is set to position?
 
Fbr
Yes it's set to position
 
Are you in the right template? Maybe another theme/module around overriding that one? You can try to debug that select: ```` $logger = new \Zend\Log\Logger(); $logger->addWriter(new \Zend\Log\Writer\Stream(BP.'/var/log/select.log')); $logger->debug((string)$_productCollection->getSelect()); ````
 
Fbr
Yes I'm in the right template. Please see my edit I add more information
 
Can you log/print your select to see what is the sort being applied? Maybe you are overriding in at the category level or as you saw with an xml
 
Fbr
10:58 AM
@SantiBM I add the result of select in my post
 
what if you try: $_productCollection->getSelect()->order('cat_index_position'‌​, 'DESC');
 
Fbr
ORDER BY change to this => ORDER BY `e`.`entity_id` DESC, `cat_index_position` ASC
(even if I set DESC in order by ASC is showing.
But product still display by entity_id first...
 
11:11 AM
is it a blank magento? do you have any modules that could be overriding the List.php block?
 
Fbr
No it is a project with some plug-in installed.
I'm new to magento and I don't know how to debug it or find which module is concerned by this
 
ok, try searching in your app/code for this string: Magento\Catalog\Block\Product\ListProduct
you want to find if a module is overriding that class
 
Fbr
Ok, there is two file that use Magento\Catalog\Block\Product\ListProduct
And One file that extends Magento\Catalog\Block\Product\ListProduct
All from third party modules.

But there is no override for getLoadedProductCollection() method
 
then maybe you should try to debug in the core file to see what is going on, if the collection there is already sorted by entity, also that class creates an event which could be acted on too: catalog_block_product_list_collection, check the file Magento\Catalog\Block\Product\ListProduct (either in vendor or app/code, depends how your magento is setup) and do some logging on the methods there like initializeProductCollection
you could also try setting the site to use the default template (blank or luma) if you are using a custom one, just to see if that is a reason or not
 
 
1 hour later…
Fbr
12:31 PM
In Magento\Catalog\Block\Product\ListProduct file in initializeProductCollection() method.
Collection is sorted by entity after this :
$this->_eventManager->dispatch(
'catalog_block_product_list_collection',
['collection' => $collection]
);

I print a select on collection before and after. Before there is not GROUP BY and after these lines there is GROUP BY entity_id
Do you know in which method sort define in admin is set to collection ?
 
maybe there is an observer listening to that event? you should try to search for the string catalog_block_product_list_collection in app/code or vendor to see what you find, maybe try commenting the event dispatch lines to check too
those are the category settings i have in a store im working now, they are working well, this is the order part of the select produced on the list:
ORDER BY `cat_index_position` asc, `cat_index`.`position` asc, `cat_index_position` asc, `cat_index`.`position` asc, `e`.`entity_id` DESC, `cat_index_position` ASC
LIMIT 12 OFFSET 24
 
Fbr
12:54 PM
There is two files with catalog_block_product_list_collection in app/code, but when I add debug on it don't print. These files are not used in this case. Only vendor one.
In Magento\Catalog\Block\Product\ListProduct when I comment dispatch lines ORDER BY is not set in _getProductCollection() but in getLoadedProductCollection() ORDER BY is set with entity_id again.

In admin I select only position in Available Product listing sort by and position is select in defailt product listing sort by
 
Also in the backend, in Stores > Configuration > Catalog > Catalog you can adjust the category sorting
 
Fbr
It was set to "date" I change it to "Position" but nothing change in frontend. I clear/flush cache and reindex
 
1:11 PM
and nothing changes after the reindex? did you check the configuration at store level?! also, do you have some positions set to a value different than 0 on the products in the category?
 
Fbr
Configuration at store level is ok.
In my category there is only two products and positions are set to 1 and 2 I also try 0 and 1
nothing change after reindex
 
Fbr
1:23 PM
Do you know exactly where category sorting define in admin was set to collection ?
it's not in Magento\Catalog\Block\Product\ListProduct
 
 
3 hours later…
4:44 PM
No I don't know about that, I think there must be an observer adding that sorting, or maybe is a default for collections in magento so its added on some deeper model
 

last day (15 days later) »