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