如何使用magento在管理类别中添加简短描述字段?

时间:2014-07-14 04:38:15

标签: magento magento-1.8

我想在管理员的管理类别中添加一个简短描述字段,并希望在前端显示描述值。我试过但无法做到这一点。 我正在添加自定义属性app/etc/modules/Atwix_CustomCategoryAttribute.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_CustomCategoryAttribute>
            <active>true</active>
            <codePool>community</codePool>
        </Atwix_CustomCategoryAttribute>
    </modules>
</config>

然后添加到app/code/community/Atwix/CustomCategoryAttribute/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_CustomCategoryAttribute>
            <version>0.0.1</version>
        </Atwix_CustomCategoryAttribute>
    </modules>

    <global>
        <resources>
            <add_category_attribute>
                <setup>
                    <module>Atwix_CustomCategoryAttribute</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute>
            <add_category_attribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_write>
            <add_category_attribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_read>
        </resources>
    </global>
</config>

然后添加到app/code/community/Atwix/CustomCategoryAttribute/sql/add_category_attribute/mysql4-install-0.0.1.php

<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_attribute', array(
    'group'         => 'General',
    'input'         => 'textarea',
    'type'          => 'text',
    'label'         => 'Custom attribute',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$this->endSetup();

这将在管理类别一般部分创建一个额外的字段。现在我想在

中显示值
app/design/frontend/default/alpha/template/catalog/product/productlisting.phtml   page

所以我把这段代码

<?php if($_customAttribute = $this->getCurrentCategory()->getCustomAttribute()): ?>
    <?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'custom_attribute') ?>
<?php endif; ?>
but it shows error

致命错误:在非对象上调用成员函数getCustomAttribute()

我不知道如何在前端显示自定义属性值。 如果有人知道这一点,请帮助我。 谢谢!

1 个答案:

答案 0 :(得分:1)

不需要所有上述内容。只需放置这两个文件即可。

在以下路径中创建 mysql4-install-1.0.0.php 。 将以下代码放在以下路径中:

/app/code/local/Test/Customcatattrb/sql/customcatattrb_setup

<?php
$installer = $this;
$installer->startSetup();
$attribute  = array(
    'type' => 'text',
    'label'=> 'Deal name',
    'input' => 'textarea',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => "",
    'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'deal_name', $attribute);
$installer->endSetup();

在以下路径中创建config.xml 的的/ opt / LAMPP / htdocs中/ Magento的/应用程序/代码/本地/测试/ Customcatattrb /等

<?xml version="1.0"?>
<config>
  <modules>
    <Test_Customcatattrb>
      <version>1.0.0</version>
    </Test_Customcatattrb>
  </modules>
    <global>
      <resources>
          <customcatattrb_setup>
            <setup>
              <module>Test_Customcatattrb</module>
              <class>Mage_Eav_Model_Entity_Setup</class>
            </setup>
            <connection>
              <use>default_setup</use>
            </connection>
          </customcatattrb_setup>
      </resources>
    </global>
</config>

我认为您已了解 app / etc / modules 中的 Test_Customcatattrb.xml 。现在只需转到管理员并检查。将在通用信息块中以“Deal Name”命名一个新字段。