11

I want to display categories tree on frontend like admin default categories tree.

Need to display category tree structure in my custom module and content area for frontend side.

Any Help would be appreciated.

Thanks.

4

1 Answer 1

5

1) go to "app" from root directory of Magento 2 and create new directory code. Then create two more directories in app/code, Namespace and Module Name. The final directory will look like this: app/code/Demo/CategoryTree.

Demo as Namespace and CategoryTree as module name.

2) create "module.xml" file in app/code/Demo/CategoryTree/etc and paste below code into the file:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Demo_CategoryTree" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

3) create "route.xml" file in app/code/Demo/CategoryTree/etc/frontend and paste below code into the file:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="categorytree" frontName="categorytree">
            <module name="Demo_CategoryTree" />
        </route>
    </router>
</config>

4) create "registration.php" file in app/code/Demo/CategoryTree and paste below code in file:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Demo_CategoryTree',
    __DIR__
);

5) create "Index.php" file in app/code/Demo/CategoryTree/Controller/Index and paste below code into the file:

<?php
/**
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Demo\CategoryTree\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Renders CATEGORYTREE Index page
     *
     * @param string|null $coreRoute
     * @return \Magento\Framework\Controller\Result\Forward
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute($coreRoute = null)
    {
        $resultPage =  $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('CategoryTree'));
        return $resultPage;
    }
}

6) create "categorytree_index_index.xml" file in app/code/Demo/CategoryTree/view/frontend/layout and paste below code into the file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <head>
        <css src="extjs/resources/css/ext-all.css"/>
        <css src="extjs/resources/css/ytheme-magento.css"/>
    </head>
    <body>
        <referenceContainer name="sidebar.additional">
            <block class="Magento\Catalog\Block\Adminhtml\Category\Tree" name="category.tree" template="Demo_CategoryTree::catalog/category/tree.phtml"/>
        </referenceContainer>
    </body>
</page>

7) copy from vendor/magento/module-catalog/view/adminhtml/templates/catalog/category/tree.phtml to app/code/Demo/CategoryTree/view/frontend/templates/catalog/category

8) create "requirejs-config.js" file in app/code/Demo/CategoryTree/view/frontend and paste below code into the file:

var config = {
    "shim": {
        "extjs/ext-tree": [
            "prototype"
        ],
        "extjs/ext-tree-checkbox": [
            "extjs/ext-tree",
            "extjs/defaults"
        ]
    }
};

9) Run below commands in the root directory:

php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:static-content:deploy

10) run url like this "http://local-magento.com/categorytree/index/index" so you will get output like below.

enter image description here

1

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.