On the Product page of Magento, I want to get the product name, its category name, and sub category name in 开发者_如何学Cthe 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);
}
}
}
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
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
精彩评论