Change Magento Subcategories in Side Layered Navig

2019-07-25 11:38发布

I was wondering if there is any way to change URLs of sidebar navigation to point to actual categories.

For example there is a parent category widgets with subcategories Computer widgets, Laptop widgets, Phone widgets. If you navigate through main navigation to phone widget SEO friendly URL will be www.example.com/widgets/phone-widgets, but if you click on widget category, by default you going to have sidebar filtering navigation with categories Computer widgets, Laptop widgets, Phone widgets. If, on this page you will click on Phone widgets, the URL of the page will be www.example.com/widgets?cat=3.

Is there any way to make those side category links to point to www.example.com/widgets/phone-widgets instead of www.example.com/widgets?cat=3? I would really like to do it through Magento code rather than extension or 301 redirect.

Thank you in advance!

3条回答
Bombasti
2楼-- · 2019-07-25 11:58

You can override getUrl() method of Mage_Catalog_Model_Layer_Filter_Item.

Here is example:

public function getUrl()
{
  $value = $this->getValue();
  $query = array(
      $this->getFilter()->getRequestVar() => $value,
      Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
  );
  $url = Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
  if ($this->getFilter()->getRequestVar() != 'cat') {
    return $url;
  }

  // Change ?cat=4 for real category url if no additional filters are present
  // www.x.com/y?cat=4
  // =>
  // www.x.com/y/z

  $pos = strpos($url, '&');
  if ($pos !== false) {
    return $url;
  } else {
    // no additional filters
    return Mage::getModel('catalog/category')->load($value)->getUrl();
  }
}
查看更多
欢心
3楼-- · 2019-07-25 12:06

I created a module that solves this error.

https://github.com/jruzafa/Devopensource_LayerCatSeo

Thanks,

查看更多
倾城 Initia
4楼-- · 2019-07-25 12:18

Use this code to load the given category (by ID)

$category = Mage::getModel('catalog/category')->load($categoryId);

Use this to get friendly URL

$category->getCategoryUrl();
查看更多
登录 后发表回答