Catalog Category Tree – problem with loading tree nodes in Magento 2

Posted on

If your store has a lot of categories and trees, you may have trouble loading the categories page. Magento 2 has three tier loading by default.

I am creating a simple module to implement a plugin.

Create di.xml file – /app/code/RewriteMagento/Catalog/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Adminhtml\Category\AbstractCategory">
        <plugin name="RewriteMagento_Catalog_Plugin_Block_Adminhtml_Category_AbstractCategory"
                type="RewriteMagento\Catalog\Plugin\Block\Adminhtml\Category\AbstractCategory"
                sortOrder="10" disabled="false"/>
    </type>
</config>

And create plugin – app/code/RewriteMagento/Catalog/Plugin/Block/Adminhtml/Category/AbstractCategory.php

<?php
declare(strict_types=1);

namespace RewriteMagento\Catalog\Plugin\Block\Adminhtml\Category;

class AbstractCategory
{
    public function beforeGetRoot(
        \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory $subject,
        $parentNodeCategory = null,
        $recursionLevel = 1
    ) {
        return [$parentNodeCategory, $recursionLevel];
    }
}

Module repository on GitHub – coming soon.

Tested in Magento 2.4.3