从Magento 1.9.2.3中的URL中的子类别中删除父类别

时间:2016-04-19 06:42:24

标签: magento url-rewriting categories

Magento版本是1.9.2.3。

我尝试过以下解决方案,但对我来说并不适用。

步骤1:转到app / code / core / Mage / Catalog / Model / Url.php将此文件复制到app / code / local / Mage / Catalog / Model / Url.php,因为它是核心Magento文件,以便我们必须将该文件复制到app / code / local。

步骤2:现在在Magento ver中搜索第698行的getCategoryRequestPath($ category,$ parentPath)。 1.9.1.0

步骤3:在第717行周围搜索if(null === $ parentPath)并按如下方式对该行进行注释:

/* if (null === $parentPath) {
$parentPath = $this->getResource()->getCategoryParentPath($category);
}
elseif ($parentPath == '/'){*/
$parentPath = ''; //DO NOT Comment this line
//} 

步骤4:现在转到Magento管理员和清除缓存系统 - >缓存管理和重新索引系统 - >索引管理数据。

第5步:刷新浏览器缓存并再次导航,现在您只会看到网址中的子类别而不是父类别,就像http://www.abcxyz.com/test-category-level-1-3.html一样 - 请参阅:http://www.expertwebadvisor.com/remove-parent-category-path-from-sub-category-url-in-magento/#sthash.cy3HvxwW.dpuf

我也尝试过这个对我不起作用的解决方案。

假设您要从网址中删除父类别路径,例如Url-> www.domain.com/cat1/cat2到www.domain.com/cat2

用法:

Go to the Magento Admin Panel -> System -> Configuration -> Catalog -> Seo Options
Select yes or no from "Use Parent Category Path for Category URLs"
Refresh category url index.

选项YES => www.domain.com/cat1/cat2

选项NO => www.domain.com/cat2

如果您有其他解决方案,请帮助我

1 个答案:

答案 0 :(得分:1)

你的第一个选择需要工作。检查是否覆盖" Mage_Catalog_Model_Url"工作正常。在方法中尝试die()。

我为特定的类别ID做了这个。我需要删除ID为' 4'的类别。来自每个类别的URL。以下是代码:

public function getCategoryRequestPath($category, $parentPath)
{
    $storeId = $category->getStoreId();
    $idPath  = $this->generatePath('id', null, $category);
    $suffix  = $this->getCategoryUrlSuffix($storeId);

    if (isset($this->_rewrites[$idPath])) {
        $this->_rewrite = $this->_rewrites[$idPath];
        $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath();
    }

    if ($category->getUrlKey() == '') {
        $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName());
    }
    else {
        $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey());
    }

    $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId());
    if (null === $parentPath && $category->getParentId() != 4) {
        $parentPath = $this->getResource()->getCategoryParentPath($category);
    }
    elseif($category->getParentId() == 4){
        $parentPath = '';
    }
    elseif ($parentPath == '/') {
        $parentPath = '';
    }
    $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath,
        true, $category->getStoreId());

    $requestPath = $parentPath . $urlKey . $categoryUrlSuffix;
    if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) {
        return $existingRequestPath;
    }

    if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) {
        return $requestPath;
    }

    return $this->getUnusedPath($category->getStoreId(), $requestPath,
        $this->generatePath('id', null, $category)
    );
}