AndreyZ
AndreyZ

Reputation: 31

Change Magento Subcategories in Side Layered Navigation to Point to Actual Categories

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!

Upvotes: 1

Views: 2043

Answers (3)

mati
mati

Reputation: 129

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();
  }
}

Upvotes: 1

jruzafa
jruzafa

Reputation: 4266

I created a module that solves this error.

https://github.com/jruzafa/Devopensource_LayerCatSeo

Thanks,

Upvotes: 2

pzirkind
pzirkind

Reputation: 2338

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();

Upvotes: 1

Related Questions