Magento - Meta Description Tag on Product View Page

Posted by Damodar Bashyal on October 27, 2012

 

After installing facebook all in one extension, QA team was testing facebook send button. Then they found that send button had some funny description. When i checked page source, i found that meta description was using product description which had html formats. If meta description is filled in there will be no such issue but if left empty, it takes description from product.

As core Magento doesn't strip html tags for meta description, let's override it to suit our need:

First create file:

app\code\community\Technooze\Tcatalog\Block\Product\View.php

And add this code:

<?php
/**
* Technooze_Tcatalog extension
*
* 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
*
* @category   Technooze
* @package    Technooze_Tcatalog
* @copyright  Copyright (c) 2008 Technooze LLC
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
 
/**
* Product View block
*
* @category Technooze
* @package  Technooze_Tcatalog
* @module   Tcatalog
* @author   Damodar Bashyal
*/
class Technooze_Tcatalog_Block_Product_View extends Mage_Catalog_Block_Product_View
{
    /**
     * Core magento code doesn't replace html tags
     * so, this function prepares the meta description before it's used
     *  Note:: you can remove this override if core team fixes it.
     *
     * @return Technooze_Tcatalog_Block_Product_View
     */
    protected function _prepareLayout()
    {
        $product = $this->getProduct();
        $description = $product->getMetaDescription();
        if(empty($description)){
            $description = $product->getDescription();
            $description = trim(preg_replace('/\s+/', ' ', preg_replace('/\r|\n/', ' ', strip_tags(html_entity_decode($description)))));
            $product->setMetaDescription(Mage::helper('core/string')->substr($description, 0, 255));
        }
 
        return parent::_prepareLayout();
    }
}

And then create another file:

app\code\community\Technooze\Tcatalog\etc\config.xml

With code as below:

<?xml version="1.0"?>
<!--
/**
* @category   Technooze
* @package    Technooze_Tcatalog
* @author     ModuleCreator
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Tcatalog>
            <version>0.1.0</version>
        </Technooze_Tcatalog>
    </modules>
    <global>
        <blocks>
            <tcatalog>
                <class>Technooze_Tcatalog_Block</class>
            </tcatalog>
            <catalog>
                <rewrite>
                    <product_view>Technooze_Tcatalog_Block_Product_View</product_view>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

Finally:

app\etc\modules\Technooze_Tcatalog.xml

And code:

<?xml version="1.0"?>
<!--
/**
* @category   Technooze
* @package    Technooze_Tcatalog
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Tcatalog>
            <active>true</active>
            <codePool>community</codePool>
        </Technooze_Tcatalog>
    </modules>
</config>

You should be getting clean meta description now.

All code can be downloaded from: https://github.com/dbashyal/magento-product-view-meta

If this helped you, Please like us on facebook and +1 on google plus.

 
not published on website


QR Code: Magento - Meta Description Tag on Product View Page