如何使用EAV AddAttribute设置默认值

时间:2012-03-16 09:30:24

标签: magento select attributes option entity-attribute-value

我想在magento中为我的产品设置一个新的属性集。此属性应该是某些选项的选择类型。

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'varchar',
    'input'             => 'select',
    #'backend'          => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => '',
    #'default'          => 1,
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'visible_in_advanced_search' => false,
    'unique'            => false,
    'option' => array(
        'value' => array( 
            'optionone' => array( 'O' ),
            'optiontwo' => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
));

如何将optionthree设置为默认值?

3 个答案:

答案 0 :(得分:6)

有同样的问题。我的解决方案:

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'int',
    'input'             => 'select',
   #'backend'           => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => 'eav/entity_attribute_source_table',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'visible_in_advanced_search' => false,

    'option' => array(
        'value' => array( 
            'optionone'   => array( 'O' ),
            'optiontwo'   => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
));

请注意不同的类型(int而不是varchar)和来源(eav/entity_attribute_source_table)。这是Magento代表典型选择属性的方式。现在您可以设置默认值,如下所示:

 $model = Mage::getModel('eav/entity_attribute')
     ->load($installer->getAttributeId('catalog_product', 'reserve'));
 $model
     ->setDefaultValue($model->getSource()->getOptionId('Keine Angabe'))
     ->save();

答案 1 :(得分:4)

请使用此脚本: -

$installer->addAttribute('catalog_product', 'reserve', array(
    'backend_label'     => 'Attribute Reserve',
    'type'              => 'varchar',
    'input'             => 'select',
    #'backend'          => 'eav/entity_attribute_source_boolean',
    'frontend'          => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'visible_in_advanced_search' => false,

    'option' => array(
        'value' => array( 
            'optionone'   => array( 'O' ),
            'optiontwo'   => array( 'P' ),
            'optionthree' => array( 'Kein Angabe' ),
        )
    ),
    /**
     * This will set the default values,
     * as "array" data type is being used to set proper default value
     */
    'default' => array(
        'optionthree'
    ),
));

希望它有所帮助。

答案 2 :(得分:-1)

导航到目录>管理属性以创建新属性并管理属性值以创建新属性集。enter image description here

请查看截图