Google+

Pages

Monday, October 29, 2012

Magento: How to remove parent category path from sub category url in Magento

Hi friends,

Today I would like to tell you, how you can remove parent category path from sub category url in Magento.

To do so, Go to:
app/code/core/Mage/Catalog/Model folder and copy Url.php file to override it in your local package.

Open this file (Url.php) and find the following:
         public function getCategoryRequestPath($category, $parentPath)

With in this function, find the following code:

        if (null === $parentPath) {
            $parentPath = $this->getResource()->getCategoryParentPath($category);
        }
        elseif ($parentPath == '/') {
            $parentPath = '';
        }
and comment it like this:

 //     if (null === $parentPath) {
 //         $parentPath = $this->getResource()->getCategoryParentPath($category);
 //     }
 //     elseif ($parentPath == '/') {
            $parentPath = '';
 //       }
and save the file.

Note: It should be noted that the code $parentPath = ''; in else part should not be commented.
Now login to admin panel of your site, and go to:
System->Config->Index Management and click on "select all" then select Reindex Data from the Action Dropdown, then click on submit.

That's all.. Enjoy.. :)


5 comments:

chiiiiing said...

Hello, just wanted to say thank you very much for sharing this information. I have looked all over for instructions on how to remove parent category (they were affecting the paths when I entered a subcategory and tried to navigate out). Your instruction was the clearest, because the other instructions I found pointed out the line # but did no mention what text to find at those lines (and apparently different versions of Magento had different codes on different lines). SO THANK YOU. You helped me a lot! :D

Matt Dazz said...

Worked brilliantly thanks. Refreshing to see a blog post that doesn't tell me to edit Core files directly as well!

abanoubb labeebb said...

thanks for this article ,it has been very helpful but i suffer from another issue
the url in the form of :
http://www.website.com/category/sub-categry/product.html
i want it in the form of :
http://www.website.com/product.html

i try the admin setting "use category name in url"
but it didn't work
so, please help me my mail is dev@bestbuyegypt.com

MOHIT KUMAR ARORA said...

To do this, go to same file. Find

function getProductRequestPath($product, $category)

Here you will find code like this:

if ($category->getLevel() > 1) {
// To ensure, that category has path either from attribute or generated now
$this->_addCategoryUrlPath($category);
$categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),false, $storeId);
$requestPath = $categoryUrl . '/' . $urlKey;
} else {
$requestPath = $urlKey;
}


Just replace this block of code with:


//if ($category->getLevel() > 1) {
// To ensure, that category has path either from attribute or generated now
// $this->_addCategoryUrlPath($category);
//$categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
// false, $storeId);
//$requestPath = $categoryUrl . '/' . $urlKey;
//} else {
$requestPath = $urlKey;
//}

Now login to admin panel of your site, and go to:
System->Config->Index Management and click on "select all" then select Reindex Data from the Action Dropdown, then click on submit.

That's all.. Enjoy.. :)

Janki Dave said...

Kudos, thanks you so much for such a easy steps, i made it,. thanks,, working very fine now.!