magento 2 categories sort order

Magento 2 admin catalog product edit form – categories order

I have recently been doing some developments in Magento 2 and have found out some inconsistency in the product edit form in the admin section.

The desired sort order of the category should be the default position one. However in the product edit admin catalog product edit form, the sort order is not defined therefore, it appears in the way it is returned from the database. This is not very user-friendly especially if you have about 4000 categories to deal with.

Magento 2 admin product edit form category sort order - backend Magento 2 natural - original  category sort order - frontend
admin catalog product edit form – categories order categories order on frontend

Thus to obtain same layout, we have made changed in following pre. You can also create pre pool and add the pre there, but this is a quick fix

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) {.

So the final code should look like as follows



$collection->addAttributeToSort('position');

        foreach ($collection as $category) {
            foreach ([$category->getId(), $category->getParentId()] as $categoryId) {
                if (!isset($categoryById[$categoryId])) {
                    $categoryById[$categoryId] = ['value' => $categoryId];
                }
            }

            $categoryById[$category->getId()]['is_active'] = $category->getIsActive();
            $categoryById[$category->getId()]['label'] = $category->getName();
            $categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
        }

        $this->getCacheManager()->save(
            $this->serializer->serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
            self::CATEGORY_TREE_ID . '_' . $filter,
            [
                \Magento\Catalog\Model\Category::CACHE_TAG,
                \Magento\Framework\App\Cache\Type\Block::CACHE_TAG
            ]
        );

        return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];

Thus my final version looks as following

Advertisements