如何使用EAV AddAttribute设置默认值

koe*_*nig 3 select attributes magento 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' ),
        )
    ),
));
Run Code Online (Sandbox Code Playgroud)

如何设置optionthree为默认值?

fxx*_*fxx 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' ),
        )
    ),
));
Run Code Online (Sandbox Code Playgroud)

注意不同的类型(int而不是varchar)和source(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();
Run Code Online (Sandbox Code Playgroud)