在Magento中为类别编辑页面添加额外的选项卡

bei*_*lex 0 magento

我正在尝试在类别编辑页面的顶部添加一个额外的选项卡.默认值为:常规信息,显示设置,自定义设计和类别产品.

所以我创建了一个新模块来重写生成选项卡的块.以下是config.xml的相关代码段:

    <blocks>

        <adminhtml>

            <rewrite>

                <catalog_category_tabs>

                    MyNamespace_MyModule_Block_Catalog_Category_Tabs

                </catalog_category_tabs>

            </rewrite>

        </adminhtml>

    </blocks>
Run Code Online (Sandbox Code Playgroud)

这是我的块覆盖默认的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意额外的标签代码:

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

但是,屏幕的右侧只是空白.类别树仍然存在,但单击类别会在Firebug中出现此Javascript错误:ReferenceError: category_info_tabsJsTabs is not defined

更新:在阅读了这个重复的问题和aswer之后,看起来我已经完成了所有事情.我缺少一些布局代码吗?

任何帮助都非常受欢迎.

小智 6

  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>
Run Code Online (Sandbox Code Playgroud)

tzunghaor_customtab/observer在observer中引用了<class>前缀<models>,因此它引用了/app/code/local/Tzunghaor/Customtab/Model/Observer.php中的Tzunghaor_Customtab_Model_Observer:

<?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'
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)