4

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.

3

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 '17 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 '17 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/… – CompactCode Nov 3 '17 at 4:05
0

Thanks for your solution.

I followed your instructions and it works well.

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

https://webdevelopmentsupport.net/2018/11/09/magento-2-admin-catalog-product-edit-form-categories-order/

Following are the steps

My Magento version is community 2.2.6

Open

vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php

Go to line 375 and add the following code

$collection->addAttributeToSort('position');

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 – Mike Sheward Apr 26 at 10:05
  • Thanks. I will try to create plugin – Sudeep Talati Aug 8 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.