下拉属性 - Magento

时间:2012-07-23 09:33:21

标签: magento attributes

我需要类别中的下拉属性。我尝试在安装程序中执行此操作:

    $installer = $this;

$installer->startSetup();

$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'priority', array(
    'type' => 'varchar',
    'label' => 'Priority2',
    'input' => 'select',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'option' => array(
            'value' => array(
                0 => array(''),
                1 => array('normal'),
                2 => array('grouping'),
                3 => array('highlighted'),
                4 => array('trendy'),
            ),
        ),
    'default' => array(0),
));

$installer->addAttributeToGroup(
        $entityTypeId, $attributeSetId, $attributeGroupId, 'priority', '11'
);

$attributeId = $installer->getAttributeId($entityTypeId, 'priority');

$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
    SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
        FROM `{$installer->getTable('catalog_category_entity')}`;
");

Mage::getModel('catalog/category')
        ->load(1)
        ->setImportedCatId(0)
        ->setInitialSetupFlag(true)
        ->save();

Mage::getModel('catalog/category')
        ->load(2)
        ->setImportedCatId(0)
        ->setInitialSetupFlag(true)
        ->save();

$installer->endSetup();

但它不起作用。 Catalog-> categories-> Manage Categories中没有显示任何内容。我只能创建像这样的textfield属性:

$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar',
'label' => 'Priority2',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',

));

你有什么想法吗? 求助。

EDIT。

我还将SQL查询绑定在这样:

INSERT INTO eav_attribute (entity_type_id, attribute_code, attribute_model, backend_model, backend_type, backend_table, frontend_model, frontend_input, frontend_label, frontend_class, source_model, is_required, is_user_defined, default_value, is_unique, note) VALUES
    (9, 'priority', NULL, 'NULL', 'int', 'NULL', 'NULL', 'select', 'Priority', NULL, 'eav/entity_attribute_source_table', 1, 0, 0, 0, '');
    INSERT INTO eav_attribute_option (option_id,attribute_id,sort_order) VALUES
    (1500,282,0),
    (1501,282,1),
    (1502,282,2),
    (1503,282,3),
    (1504,282,4);
    INSERT INTO eav_attribute_option_value (value_id,option_id,store_id,value) VALUES
    (201,1500,0,''),
    (202,1501,0,'normal'),
    (203,1502,0,'grouping'),
    (204,1503,0,'highlighted'),
    (205,1504,0,'trendy');

    INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES
    (9, 12, 7, (SELECT atribute_id FROM eav_attribute WHERE attribute_code='priority'), 2);

2 个答案:

答案 0 :(得分:2)

可以使用模块安装程序:

$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'myattribute', array(
    'type' => 'varchar',
    'label' => 'My attribute',
    'source' => 'module/category_attribute_myattribute',
    'backend' => 'module/category_attribute_backend_myattribute',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'input' => 'select',
    'visible' => true,
    'required' => true,
    'user_defined' => false,
    'used_in_product_listing' => false,
    'group' => 'Group name',
    'default' => Module_Catalog_Model_Category_Attribute_Myattribute::DEFAULT_Groupname,
));

希望它可以帮到某人;)

答案 1 :(得分:0)

请参阅教程中的文件结构和分步说明。 http://www.pearlbells.co.uk/how-to-add-custom-dropdown-attribute-to-magento-category-section/

<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
'group'         => 'General Information',
'input'         => 'select',
'type'          => 'text',
'label'         => 'Custom Dropdown',
'backend'       => '',
'visible'       => true,
'required'      => false,
'visible_on_front' => true,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'customfeaturedattribute/source_custom',
));

$this->endSetup();

在模型类中添加选项。

public function getAllOptions()
{
    $options = array(
        1 => 'One',
        2 => 'Two',
        3 => 'Three'
    );

    return $options;
}