Working with Magento EE v2.1.7.

From the admin product edit page (URL/route: admin/catalog/product/edit/), I have an option to assign products to categories.

enter image description here

I'm curious about the ordering of these suggested categories as the ordering does not currently reflect what I can see from the category management section of the admin (URL/route: admin/catalog/category/edit/).

Does anyone happen to know the sort order for the product edit portion of the admin? It mostly lines up which is interesting but it doesn't 100% reflect. Tried reindexing just to see if something was amiss.


2 Answers 2


I seems to me that on the product add/edit screen the categories are shown in the way they are created.
Or better yet, in the order that mysql returns the records by default because there is no sort order specified.
The categories are returned by this method Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories::getCategoriesTree.
There is some strange logic in there that I don't fully understand, but there is no sort order specified anywhere for the categories.

If I add this line $collection->addAttributeToSort('position'); right before foreach ($collection as $category) { and clear the cache (because the category tree is cached) then the categories seem to appear in the order they are set in the category management section.

Maybe it is worth adding this as an issue on github.

  • is it possible to rewrite in own module without affecting core?
    – Sathish
    Oct 31, 2017 at 8:30
  • 2
    It's possible. You can write an around plugin for the method I mentioned that contains the full code of the method with the extra line I mentioned.
    – Marius
    Oct 31, 2017 at 8:38
  • This is a valid answer. Maybe include the plugin interceptors magento devdocs page : devdocs.magento.com/guides/v2.1/extension-dev-guide/… Nov 3, 2017 at 4:05

Thanks for your solution.

I followed your instructions and it works well.

I have also added a blog with images to support your answer


Following are the steps

My Magento version is community 2.2.6



Go to line 375 and add the following code


Make sure to add this line before

foreach ($collection as $category) {
  • This may work however you ideally should wrap this code in a plugin so not to override the core directly Apr 26, 2019 at 10:05
  • Thanks. I will try to create plugin Aug 8, 2019 at 7:03
  • Works for me! Thanks
    – mapaladiya
    Jan 3, 2020 at 6:18
  • Annoyingly, this is still present in 2.3 branch
    – elfling
    Jan 25, 2022 at 14:23
  • and also the 2.4 branch
    – elfling
    Jan 25, 2022 at 14:48

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.