duanbushi1479 2015-10-31 03:29
浏览 7
已采纳

在目录 - >管理类别 - >显示设置中创建新属性字段

Im trying to add attribute field in the Display settings in Manage Categories. I found this tutorial on the web http://www.marketingadept.com/blog/magento-developers-add-a-custom-field-to-the-category-admin-page/

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CmsBlock>

<version>0.0.1</version>
</CmsBlock>
</modules>
<global>
<resources>
<cmsblock_setup>
<setup>
<module>CmsBlock</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
<connection>
<use>core_setup</use>
</connection>
</setup>
</cmsblock_setup>
<cms_block_setup_write>
<connection>
<use>core_write</use>
</connection>
</cms_block_setup_write>
<cms_block_setup_read>
<connection>
<use>core_read</use>
</connection>
</cms_block_setup_read>
</resources>
</global>
</config>

mysql4-install-0.0.1.php

$installer = $this;
$installer->startSetup();


$entityTypeId     = $installer->getEntityTypeId('catalog_category');


$attributeSetId   = $installer->getAttributeSetId($entityTypeId);

$attributeGroupId = $installer->getAttributeGroupId($entityTypeId, $attributeSetId,5);

$installer->addAttribute('catalog_category', 'cms_block',  array(
    'type'     => 'varchar', /* Type - see eav_entity_* for the different types */
    'label'    => 'CMS Block', /* Your label */
    'input'    => 'select', /* This refers to the type of form field should display*/
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => TRUE,
    'required'          => FALSE,
    'user_defined'      => FALSE,
    'option'           => array('values'=> array('Option 1','Option 2'))
));

$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'cms_block',
    '52' 
);
$installer->endSetup();

the installer script does run as I checked it in the core_resources table but the field does not display on the Display Settings Tab. Can anyone help me with this?

  • 写回答

1条回答 默认 最新

  • duandao8607 2015-11-02 07:25
    关注

    You can add a dropdown in manage categories section. Please do the following :-

    app/etc/modules/Magpedia_Categorytab.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Magpedia_Categorytab>
                <active>true</active>
                <codePool>local</codePool>
                <version>0.1.0</version>
            </Magpedia_Categorytab>
        </modules>
    </config>
    

    app/code/local/Magpedia/Categorytab/etc/config.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Magpedia_Categorytab>
          <version>0.1.0</version>
        </Magpedia_Categorytab>
      </modules>
      <global>
        <helpers>
          <categorytab>
        <class>Magpedia_Categorytab_Helper</class>
          </categorytab>
        </helpers>
        <models>
          <categorytab>
            <class>Magpedia_Categorytab_Model</class>
            <resourceModel>categorytab_mysql4</resourceModel>
          </categorytab>
        </models>
        <resources>
          <categoryattribute1446446121_setup>
            <setup>
              <module>Magpedia_Categorytab</module>
              <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
            <connection>
              <use>core_setup</use>
            </connection>
          </categoryattribute1446446121_setup>
          <categoryattribute1446446121_write>
            <connection>
              <use>core_write</use>
            </connection>
          </categoryattribute1446446121_write>
          <categoryattribute1446446121_read>
            <connection>
              <use>core_read</use>
            </connection>
          </categoryattribute1446446121_read>
        </resources>
      </global>
    </config> 
    

    app/code/local/Magpedia/Categorytab/Helper/Data.php

    <?php
    class Magpedia_Categorytab_Helper_Data extends Mage_Core_Helper_Abstract
    {
    }
    

    app/code/local/Magpedia/Categorytab/Model/Eav/Entity/Attribute/Source/Categoryoptions14464461210.php

    <?php
    class Magpedia_Categorytab_Model_Eav_Entity_Attribute_Source_Categoryoptions14464461210 extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
    {
        /**
         * Retrieve all options array
         *
         * @return array
         */
        public function getAllOptions()
        {
        if (is_null($this->_options)) {
            $this->_options = array(
    
                array(
                    "label" => Mage::helper("eav")->__("cms block 1"),
                    "value" =>  1
                ),
    
                array(
                    "label" => Mage::helper("eav")->__("cms block 1"),
                    "value" =>  2
                ),
    
                array(
                    "label" => Mage::helper("eav")->__("cms block 1"),
                    "value" =>  3
                ),
    
                array(
                    "label" => Mage::helper("eav")->__("cms block 1"),
                    "value" =>  4
                ),
    
            );
        }
        return $this->_options;
        }
    
        /**
         * Retrieve option array
         *
         * @return array
         */
        public function getOptionArray()
        {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
        }
    
        /**
         * Get a text for option value
         *
         * @param string|integer $value
         * @return string
         */
        public function getOptionText($value)
        {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
        }
    
        /**
         * Retrieve Column(s) for Flat
         *
         * @return array
         */
        public function getFlatColums()
        {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            "type"      => "tinyint(1)",
            "unsigned"  => false,
            "is_null"   => true,
            "default"   => null,
            "extra"     => null
        );
    
        return $columns;
        }
    
        /**
         * Retrieve Indexes(s) for Flat
         *
         * @return array
         */
        public function getFlatIndexes()
        {
        $indexes = array();
    
        $index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            "type"      => "index",
            "fields"    => array($this->getAttribute()->getAttributeCode())
        );
    
        return $indexes;
        }
    
        /**
         * Retrieve Select For Flat Attribute update
         *
         * @param int $store
         * @return Varien_Db_Select|null
         */
        public function getFlatUpdateSelect($store)
        {
        return Mage::getResourceModel("eav/entity_attribute")
            ->getFlatUpdateSelect($this->getAttribute(), $store);
        }
    }
    

    app/code/local/Magpedia/Categorytab/sql/categoryattribute1446446121_setup/mysql4-install-0.1.0.php

        <?php
    $installer = $this;
    $installer->startSetup();
    
    
    $installer->addAttribute("catalog_category", "custom_cms_block",  array(
        "type"     => "int",
        "backend"  => "",
        "frontend" => "",
        "label"    => "CMS Block",
        "input"    => "select",
        "class"    => "",
        "source"   => "categorytab/eav_entity_attribute_source_categoryoptions14464461210",
        "global"   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        "visible"  => true,
        "required" => false,
        "user_defined"  => false,
        "default" => "",
        "searchable" => false,
        "filterable" => false,
        "comparable" => false,
    
        "visible_on_front"  => false,
        "unique"     => false,
        "note"       => ""
    
        ));
    $installer->endSetup();
    

    Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况