magentoの製品ページで、メタキーワードタグの製品名、そのカテゴリ名、およびサブカテゴリ名を取得したいと思います。
助けてください!!
前もって感謝します。
製品にはすでにMetaKeyword値が付加されているため、オブザーバーを使用してその値を目立たないように拡張できます。このメソッドには、コアクラスの拡張は含まれません
これを試して:
/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);
}
}
}
Mage_Catalog_Block_Product_Viewクラス、特に__preparelayout()メソッドを書き直す必要があります。
上書きする_prepareLayoutメソッドに次のコードを追加するだけです。
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();
}
メタキーワードとメタディスクリプションを上記のように設定することが重要です。設定しないと、親クラスによって再度上書きされます。
よろしく、ケニー
商品詳細ページのことですか?はいの場合は非常に単純です
特定の製品ページに移動すると、[メタ情報]タブが表示されます。ここに追加できます
Meta Title, Meta Keywords, Meta Description