2011-09-30 37 views
1

在magento的產品頁面上,我想要獲取元關鍵字標籤中的產品名稱,類別名稱和子類別名稱。動態獲取magento meta關鍵字?

請幫忙!!

在此先感謝。

+0

僅供參考,元關鍵詞標籤對您的網頁排名沒有影響。沒有主要的搜索引擎使用它們了。 –

回答

3

由於產品已經有一個附加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> 

/應用/代碼/本地/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); 
     } 
    } 

} 
0

您是否想了解產品詳細頁面?如果是指非常簡單

轉到您的具體產品頁面,應該有標籤「元信息」。在這裏,你可以添加

Meta Title, Meta Keywords, Meta Description 
+0

我想他想動態添加 –

+0

如何檢查我的所有類別是否有元標記? – Rathinam

1

你不得不重新改寫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(); 
} 

您設置的metakeyword和metadescription它會再次被父類被覆蓋在它上面定律描述的方式,否則是很重要的( ES)。

問候, 肯尼