Get correct Magento multi level categories page title and canonical url

Hello, I noticed there was a problem with the page titles and canonical urls for multi-level categories pages, for example if I have a category like this:

ebook/fiction/general

Magento 1.5.1.0 puts a wrong canonical url like this:

http://example.com/ebook

while it should be:

http://example.com/ebook/fiction/general

So we need a patch to fix this behavior and I will show you below. In the same time I needed to put a page title for category which hasn’t a default one.

If you haven’t write a page title for every category you have, Magento simply places the default store title, but i think this is wrong, because you know SEO is important, and page title is the most important thing on a website.

So I add a correct page title for categories without it by patching the same file for the fix above and you will have a page title with every category level names like this:

<title>Ebook - Fiction - General</title>

that’s much better then the default store title.

How to get all of this? Very simple, just place this code in “app/code/local/Mage/Catalog/Block/Category/View.php” so you will override the default one without touching the core file, and you are done (the function touched is only the _prepareLayout):


<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Category View block
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Catalog_Block_Category_View extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
parent::_prepareLayout();

$this->getLayout()->createBlock('catalog/breadcrumbs');

if ($headBlock = $this->getLayout()->getBlock('head')) {
$category = $this->getCurrentCategory();
if ($title = $category->getMetaTitle()) {
$headBlock->setTitle($title);
}

// SBF get categories name as page title
if (!$title ) {

$title = $category->getName();

$it = 5; //Amount of iterations before script gives up

if ($category) {

while($category->getLevel() != 2 &amp;amp;&amp;amp; $it > 0) {
$category = $category->getParentCategory();
$title = $category->getName().' - '.$title;

if (!($category instanceof Mage_Catalog_Model_Category)) {
break;
}
}

}
$headBlock->setTitle($title);
};

if ($description = $category->getMetaDescription()) {
$headBlock->setDescription($description);
}
if ($keywords = $category->getMetaKeywords()) {
$headBlock->setKeywords($keywords);
}
if ($this->helper('catalog/category')->canUseCanonicalTag()) {
// SBF
$headBlock->addLinkRel('canonical', $this->getCurrentCategory()->getUrl());
}
/*
want to show rss feed in the url
*/
if ($this->IsRssCatalogEnable() &amp;amp;&amp;amp; $this->IsTopCategory()) {
$title = $this->helper('rss')->__('%s RSS Feed',$this->getCurrentCategory()->getName());
$headBlock->addItem('rss', $this->getRssLink(), 'title="'.$title.'"');
}
}

return $this;
}

public function IsRssCatalogEnable()
{
return Mage::getStoreConfig('rss/catalog/category');
}

public function IsTopCategory()
{
return $this->getCurrentCategory()->getLevel()==2;
}

public function getRssLink()
{
return Mage::getUrl('rss/catalog/category',array('cid' => $this->getCurrentCategory()->getId(), 'store_id' => Mage::app()->getStore()->getId()));
}

public function getProductListHtml()
{
return $this->getChildHtml('product_list');
}

/**
* Retrieve current category model object
*
* @return Mage_Catalog_Model_Category
*/
public function getCurrentCategory()
{
if (!$this->hasData('current_category')) {
$this->setData('current_category', Mage::registry('current_category'));
}
return $this->getData('current_category');
}

public function getCmsBlockHtml()
{
if (!$this->getData('cms_block_html')) {
$html = $this->getLayout()->createBlock('cms/block')
->setBlockId($this->getCurrentCategory()->getLandingPage())
->toHtml();
$this->setData('cms_block_html', $html);
}
return $this->getData('cms_block_html');
}

/**
* Check if category display mode is "Products Only"
* @return bool
*/
public function isProductMode()
{
return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PRODUCT;
}

/**
* Check if category display mode is "Static Block and Products"
* @return bool
*/
public function isMixedMode()
{
return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_MIXED;
}

/**
* Check if category display mode is "Static Block Only"
* For anchor category with applied filter Static Block Only mode not allowed
*
* @return bool
*/
public function isContentMode()
{
$category = $this->getCurrentCategory();
$res = false;
if ($category->getDisplayMode()==Mage_Catalog_Model_Category::DM_PAGE) {
$res = true;
if ($category->getIsAnchor()) {
$state = Mage::getSingleton('catalog/layer')->getState();
if ($state &amp;amp;&amp;amp; $state->getFilters()) {
$res = false;
}
}
}
return $res;
}
}

 


Tags: , , , , , , ,

Post simili:

Il caricabatterie universale PowerBrick di Feligan

Oggi recensisco questo magnifico prodotto della società Feligan (azienda di Reggio Emilia), il Powerbrick è il caricabatterie che vi lascerà a bocca aperta grazie alla sua flessibilità è in grado infatti di caricare tutti i device che avete con un unico apparecchio, discreto e molto funzionale. Ma andiamo al dettaglio, Powerbrick è una scatoletta dalle […]


Magento and Uservoice Single Sign-On Autenthication (Free extension)

Hello, I just wrote a small Magento extension to allow you the single sign-on between you Magento ecommerce site and your Uservoice support site. Based upon the original Uservoice documentation, you can install it (just copy the files into your Magento folder), you will find a new configuration settings under the left sidebar “USERVOICE SINGLE […]


Chrome and Android – A Potent Combination

If you’re a Google user when it comes to email, browsing the web, or using your phone, you’ll know that if there’s one thing the search-engine giant is good at, it’s integrating one Google idea into another seamlessly. Chrome and Android and two great examples, and there are a fair few examples of why you’ll […]


Magento problem with Paypal Express and new customer registration [Solved]

You know there is this problem with Magento 1.4.* and 1.5.*, if you are using Paypal Express as payment method and your customer is a new one, when she/he complete the order this is without account association (Customer Group 0 USER NOT LOGGED IN) and the new customer is not created. To solve this problem […]


AWS EC2 Manage Snapshot Backup with PHP SDK

This is a simple script based upon AWS SDK PHP to manage volume’s snapshots. It simply create one snapshot per volume (once you run it) and delete snapshot older then 1 month. Please provide your personal information by copying config-sample.inc.php into config.inc.php. Run the script backup-snapshots.php with your CRON. For example: # Auto AWS Snapshots […]


Magento add Bestseller products by date range

Here is how to get a collection of products best sold in a given date range (could be daily, monthly, weekly, etc…)


Magento adding you own custom RSS Feeds

You know you will need, before or after, to add your own custom RSS feeds to your Magento. For example, I needed to create some custom RSS Feeds to list bestseller products by date (bestseller weekly, monthly, etc…) How to do it? It’s quite complex but not so much if you follow this mini tutorial: […]


Magento adding downloadable product to cart by coding php

Probably you know it’s possible to add a product to the cart via querystring, but may be you prefer code a new module for your needs using Magento objects and methods. So adding a simple product is easy: But how to add a Downloadable product with its right link ? Just try this, it’s a […]


Magento Export products in custom XML [script]

If you have to export all or a part of your Magento products you can try this simple script which creates a products collection and writes a simple and custom XML file per product (but you can simply change it to write a single long XML file). Hope you enjoy it!


Categories