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


Creating new attribute field in Catalog->Manage Categories->Display Settings

我正在尝试在"管理类别"的"显示"设置中添加属性字段。我在网上找到了这个教程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.01.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();

安装程序脚本确实在我在core_resources表中检查时运行,但该字段没有显示在"显示设置"选项卡上。有人能帮我吗?

您可以在管理类别部分添加下拉列表。请执行以下操作:-

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/Eentity/Attribute/Source/Categoryotions14464461210.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.10.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();

感谢