Magento多级分类URL去掉父目录

在Magento模板开发中,有时候需要将多级分类的url简化,Magento的URL默认是显示多级分类的

http://www.a.com/分类1/分类2/分类3

现在需要简化为:

分类2的URL为:http://www.a.com/分类2

分类3的URL为:http://www.a.com/分类3

具体修改方法如下:

打开app/code/core/Mage/Catalog/Model/Url.php这个php文件,找到

if (null === $parentPath) {
$parentPath = $this->getResource()->getCategoryParentPath($category);
}
elseif ($parentPath == '/') {
$parentPath = '';
}

把上面的代码修改为:

//if (null === $parentPath) {
//$parentPath = $this->getResource()->getCategoryParentPath($category);
//}
//elseif ($parentPath == '/') {
$parentPath = '';
//}

其实就是把这段代码屏蔽掉了。
其中

$parentPath = $this->getResource()->getCategoryParentPath($category);

是获取分类的父级分类,下面将父级分类设置为空则在生成的URL中屏蔽掉父级
仔细观察core_url_rewrite表里面的数据 就会发现

原文地址:https://www.cnblogs.com/liuxgnu/p/3535414.html