Reputation: 39
On the Product page of Magento, I want to get the product name, its category name, and sub category name in the meta keywords tag.
Upvotes: 1
Views: 9992
Reputation: 3124
Since products already have an attached MetaKeyword value, you can use an observer to unobtrusively extend that value. This method doesn't involve extending a core class
Try this:
/app/code/local/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_YourModule>
<version>1.0.0</version>
</YourCompany_YourModule>
</modules>
<global>
<models>
<YourCompany_YourModule>
<class>YourCompany_YourModule_Model</class>
</YourCompany_YourModule>
</models>
</global>
<frontend>
<events>
<catalog_controller_product_view>
<observers>
<YourCompany_YourModule>
<class>YourCompany_YourModule/Observer</class>
<method>productView</method>
</YourCompany_YourModule>
</observers>
</catalog_controller_product_view>
</events>
</frontend>
</config>
/app/code/local/YourCompany/YourModule/Model/Observer.php
<?php
class YourCompany_YourModule_Model_Observer
{
public function productView(Varien_Event_Observer $observer)
{
$product = $observer->getEvent()->getProduct();
/* @var $product Mage_Catalog_Model_Product */
if ($product) {
$keywords = $product->getMetaKeyword();
// Add the product name
$keywords = ' ' . $product->getName();
// Add the category name
$currentCategory = Mage::registry('current_category');
if ($currentCategory && $currentCategory instanceof Mage_Catalog_Model_Category) {
$keywords = ' ' . $currentCategory->getName();
}
$product->setMetaKeyword($keywords);
}
}
}
Upvotes: 4
Reputation: 5410
You have to rewrite the Mage_Catalog_Block_Product_View class, especially the __preparelayout() method.
Simply add the following code in the _prepareLayout method you will overwrite:
protected function _prepareLayout()
{
$currentCategory = Mage::registry('current_category'); // For accessing current category information
$product = $this->getProduct();
if ($headBlock = $this->getLayout()->getBlock('head')) {
$headBlock->setTitle("Whatever you want here");
$product->setMetaKeyword("whatever, keywords, you, want, here");
$product->setMetaDescription("Whatever description you want here);
}
return parent::_prepareLayout();
}
It's important that you set the metakeyword and metadescription the way it's described above, else it will be overwritten again by the parent class(es).
Regards, Kenny
Upvotes: 1
Reputation: 16845
Are you meaning product details page ? if yes means very simple
Goto your specific product page,there should be tab "Meta Information".here you can add
Meta Title, Meta Keywords, Meta Description
Upvotes: 0