2014-01-16 60 views
0

我試圖在類別編輯頁面的頂部添加一個額外的選項卡。默認的是:常規信息,顯示設置,自定義設計和類別產品。將額外的選項卡添加到Magento中的類別編輯頁面

所以,我創建了一個新的模塊,重寫生成選項卡的塊。下面是從config.xml中相關片段:

<blocks> 

     <adminhtml> 

      <rewrite> 

       <catalog_category_tabs> 

        MyNamespace_MyModule_Block_Catalog_Category_Tabs 

       </catalog_category_tabs> 

      </rewrite> 

     </adminhtml> 

    </blocks> 

這裏是我的塊覆蓋默認的Magento一個:

class MyNamespace_MyModule_Block_Catalog_Category_Tabs extends Mage_Adminhtml_Block_Catalog_Category_Tabs 
{ 

    protected function _prepareLayout() 
    { 
     $categoryAttributes = $this->getCategory()->getAttributes(); 
     if (!$this->getCategory()->getId()) { 
      foreach ($categoryAttributes as $attribute) { 
       $default = $attribute->getDefaultValue(); 
       if ($default != '') { 
        $this->getCategory()->setData($attribute->getAttributeCode(), $default); 
       } 
      } 
     } 

     $attributeSetId  = $this->getCategory()->getDefaultAttributeSetId(); 
     /** @var $groupCollection Mage_Eav_Model_Resource_Entity_Attribute_Group_Collection */ 
     $groupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection') 
      ->setAttributeSetFilter($attributeSetId) 
      ->setSortOrder() 
      ->load(); 
     $defaultGroupId = 0; 
     foreach ($groupCollection as $group) { 
      /* @var $group Mage_Eav_Model_Entity_Attribute_Group */ 
      if ($defaultGroupId == 0 or $group->getIsDefault()) { 
       $defaultGroupId = $group->getId(); 
      } 
     } 

     foreach ($groupCollection as $group) { 
      /* @var $group Mage_Eav_Model_Entity_Attribute_Group */ 
      $attributes = array(); 
      foreach ($categoryAttributes as $attribute) { 
       /* @var $attribute Mage_Eav_Model_Entity_Attribute */ 
       if ($attribute->isInGroup($attributeSetId, $group->getId())) { 
        $attributes[] = $attribute; 
       } 
      } 

      // do not add grops without attributes 
      if (!$attributes) { 
       continue; 
      } 

      $active = $defaultGroupId == $group->getId(); 
      $block = $this->getLayout()->createBlock($this->getAttributeTabBlock(), '') 
       ->setGroup($group) 
       ->setAttributes($attributes) 
       ->setAddHiddenFields($active) 
       ->toHtml(); 
      $this->addTab('group_' . $group->getId(), array(
       'label'  => Mage::helper('catalog')->__($group->getAttributeGroupName()), 
       'content' => $block, 
       'active' => $active 
      )); 
     } 

     $this->addTab('products', array(
      'label'  => Mage::helper('catalog')->__('Category Products'), 
      'content' => $this->getLayout()->createBlock(
       'adminhtml/catalog_category_tab_product', 
       'category.product.grid' 
      )->toHtml(), 
     )); 

     // dispatch event add custom tabs 
     Mage::dispatchEvent('adminhtml_catalog_category_tabs', array(
      'tabs' => $this 
     )); 

     $this->addTab('myextratab', array(
      'label'  => Mage::helper('catalog')->__('My Extra Tab'), 
      'content' => 'Here is the contents for my extra tab' 
     ));   

     return parent::_prepareLayout(); 
    } 
} 

注意額外的標籤代碼:

 $this->addTab('myextratab', array(
      'label'  => Mage::helper('catalog')->__('My Extra Tab'), 
      'content' => 'Here is the contents for my extra tab' 
     )); 

但是,屏幕的右側只是空白。類別樹仍然存在,但點擊一個類別中的螢火蟲給這個JavaScript錯誤:ReferenceError: category_info_tabsJsTabs is not defined

UPDATE: 已經上閱讀this duplicate question and aswer所以它看起來像我所做的一切。有一些佈局代碼我錯過了嗎?

任何幫助是大規模讚賞。

回答

5
  1. 關閉緩存和編譯,如果你還沒有做過。

我寧願使用的事件觀察器,因爲我感覺不到它侵入:

我模塊config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
     <Tzunghaor_Customtab> 
      <version>0.1.0</version> 
     </Tzunghaor_Customtab> 
    </modules> 

    <global> 
     <models> 
      <tzunghaor_customtab> 
       <class>Tzunghaor_Customtab_Model</class> 
      </tzunghaor_customtab> 
     </models> 

     <events> 
      <adminhtml_catalog_category_tabs> 
       <observers> 
        <tzunghaor_customtab_observer> 
         <class>tzunghaor_customtab/observer</class> 
         <method>addCategoryTab</method> 
        </tzunghaor_customtab_observer> 
       </observers> 
      </adminhtml_catalog_category_tabs> 
     </events> 

    </global> 
</config> 

tzunghaor_customtab/observer在觀察者指<models>定義的<class>前綴,所以指的是Tzunghaor_Customtab_Model_Observer,它位於/app/code/local/Tzunghaor/Customtab/Model/Observer.php中:

<?php 
class Tzunghaor_Customtab_Model_Observer 
{ 
    /** 
    * Adds a custom tab to adminhtml category page 
    * 
    * @param Varien_Event_Observer $observer 
    */ 
    public function addCategoryTab($observer) 
    { 
     $tabs = $observer->getEvent()->getTabs(); 
     $tabs->addTab('features', array(
      'label'  => Mage::helper('catalog')->__('My Extra Tab'), 
      'content' => 'Here is the contents for my extra tab' 
     )); 
    } 
} 
相關問題