How to fix layered navigation filer URL in Magento?
April 2, 2015
Magento is really good eCommerce CMS in terms of functionality. Layered Navigation is really good feature of Magento to filter the product in frontend side. Only problem with the layered navigation filter is that it work with query string. for Example
www.yourwebsitesite.com/category.html?color=120.
Another problem is with category filter. For example we have main category "Cat1" and a sub category "sub1", we will have this url:
www.yourwebsitesite.com/cat1/sub1.html
With filter:
www.yourwebsitesite.com/cat1.html?cat=5
Obviously is an issue for SEO with duplicated content. We can fix the layered navigation filter thing by overriding the
getUrl() method in the at:
/app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php
Of Course, from a developer point of view, you can make local version of it and modify the code as explained below:
Locate the function at line 57, it should look like this:
public function getUrl()
{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
);
return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
Now you need to edit this code and replace with:
public function getUrl()
{
if($this->getFilter()->getRequestVar() == "cat"){
$category_url = Mage::getModel('catalog/category')->load($this->getValue())->getUrl();
$return = $category_url;
$request = Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
if(strpos($request,'?') !== false ){
$query_string = substr($request,strpos($request,'?'));
}
else{
$query_string = '';
}
if(!empty($query_string)){
$return .= $query_string;
}
return $return;
}
else{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
);
return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
}
Its really quick way to fix it and checked with Magento version 1.9
Feel Free to
contact me any time.