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

时间:2015-10-31 03:29:37

标签: php magento

我尝试在“管理类别”的“显示”设置中添加属性字段。我在网上发现了这个教程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安装-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();

安装程序脚本在我在 core_resources 表中检查时运行,但该字段未显示在“显示设置”选项卡上。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

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

应用的/ etc /模块/ 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>

应用/代码/本地/ 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> 

应用/代码/本地/ Magpedia / Categorytab /助手/ Data.php

<?php
class Magpedia_Categorytab_Helper_Data extends Mage_Core_Helper_Abstract
{
}

应用/代码/本地/ Magpedia / Categorytab /型号/ EAV /实体/属性/源/ 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);
    }
}

应用/代码/本地/ Magpedia / Categorytab / SQL / categoryattribute1446446121_setup / mysql4安装-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();

谢谢