我想像管理员默认类别树一样在前端显示类别树。
需要在前端模块的自定义模块和内容区域中显示类别树结构。
任何帮助,将不胜感激。
谢谢。
我想像管理员默认类别树一样在前端显示类别树。
需要在前端模块的自定义模块和内容区域中显示类别树结构。
任何帮助,将不胜感激。
谢谢。
Answers:
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 ”,这样您将获得如下输出。
好的,这就是我用来基于类别树生成菜单的方法。应该注意的是,为方便起见,我的所有类别都存储在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>