Magento 2:在前端显示管理类别树


10

我想像管理员默认类别树一样在前端显示类别树。

需要在前端模块的自定义模块和内容区域中显示类别树结构。

任何帮助,将不胜感激。

谢谢。



不仅显示类别名称,而且需要带有树的类别(与admin相同)。
Suresh Chikani

请参考:mage2.pro/t/topic/912这将为您提供帮助
Nikunj Vadariya

1
@nikunjVadariya谢谢您的建议。我看看
Suresh Chikani

Answers:


4

1)从Magento 2的根目录转到“ app”并创建新的目录代码。然后在app / code中创建两个目录,命名空间和模块名称。最终目录如下所示:app / code / Demo / CategoryTree

演示作为命名空间类别树作为模块名称。

2)在app / code / Demo / CategoryTree / etc中创建“ module.xml”文件,并将以下代码粘贴到文件中:

<?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)在app / code / Demo / CategoryTree / etc / frontend中创建“ route.xml”文件,并将以下代码粘贴到该文件中:

<?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)在app / code / Demo / CategoryTree中创建“ registration.php”文件,并将以下代码粘贴到文件中:

<?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)在app / code / Demo / CategoryTree / Controller / Index中创建“ Index.php”文件,并将以下代码粘贴到文件中:

<?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)在app / code / Demo / CategoryTree / view / frontend / layout中创建“ categorytree_index_index.xml”文件,并将以下代码粘贴到文件中:

<?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)从供应商/magento/module-catalog/view/adminhtml/templates/catalog/category/tree.phtml复制app / code / Demo / CategoryTree / view / frontend / templates / catalog / category

8)在app / code / Demo / CategoryTree / view / frontend中创建“ requirejs-config.js”文件,并将以下代码粘贴到文件中:

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

9)在根目录中运行以下命令:

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

10)像这样运行网址“ http://local-magento.com/categorytree/index/index ”,这样您将获得如下输出。

在此处输入图片说明


您好@nilesh gosai您能在这个magento.stackexchange.com/questions/249360/
Nagaraju K

1

好的,这就是我用来基于类别树生成菜单的方法。应该注意的是,为方便起见,我的所有类别都存储在ID2的默认类别下,该类别随全新安装的Magento2一起提供。如果您没有这种结构,则可以选择将其定义$soncats为要循环浏览的类别ID的数组。

<ul id="nav" class="accordion vertnav vertnav-top grid-full wide">
    <?php
$fathercat = $objectManager->create('Magento\Catalog\Model\Category')->load(2); //this is my master root category, holds all other categories so I can loop through.
$soncats = $fathercat->getChildrenCategories(); 
$catids = array(2); 
foreach ($soncats as $soncat) {
    $categoryid = $soncat->getId();
    array_push($catids,$categoryid);
}
for($i = 1; $i < count($catids); ++$i) { 
    $basic = 1;
    $catId = $catids[$i];
    $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catId);
    $subcats = $subcategory->getChildrenCategories();
    $categoryname = $subcategory->getName(); 
    $categoryurl = $subcategory->getUrl(); ?>
    <li class="level0 nav-<?php echo $i;?> level-top parent"><a href="<?php echo $categoryurl ?>" class="level-top"><?php echo $categoryname; ?><span class="caret"> </span> </a><span class="opener"> </span>
        <div class="level0-wrapper dropdown-6col" style="left: 0;">
            <div class="level0-wrapper2">
                <ul class="level0 part">
                    <?php
                    foreach ($subcats as $subcat) { 
                        if ($subcat->getIsActive()) {
                            $subcat_url = $subcat->getUrl(); 
                            $subcat_name = $subcat->getName(); ?>
                            <li class="level1 nav-1-<?php echo $basic;?> item"><a href="<?php echo $subcat_url ?>"><?php echo $subcat_name; ?></a></li>
                            <?php
                        } $basic++; } ?>
                    </ul>
                </div>
            </div>
        </div>
    </li>
    <?php } ?>
</ul>

你好约翰,看来你有未定义的变量$ addedq。
Purushotam Sangroula,

嗨,动漫,您好,谢谢您发现,adddq是先前定义的另一个变量,相对于我所讨论的项目,代码没有必要起作用
John

此代码未显示孩子的子类别
HaFiz Umer

@HaFizUmer有点奇怪,因为它应该是,但是我也不会感到惊讶,因为它是针对Magento 2.0〜的。它可能需要修改/重写任何Magento的2.1+
约翰·
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.