以编程方式为多选/选择Magento属性设置默认值

时间:2012-05-31 23:21:42

标签: magento select default-value custom-attributes multi-select

以下是我创建新的Product multiselect属性的方法:

$eav = new Mage_Catalog_Model_Resource_Setup('core_setup');

$eav->addAttribute(
    Mage_Catalog_Model_Product::ENTITY, 
    "product_country", 
    array(
        'label'                      => 'Country',
        'group'                      => 'General',
        'type'                       => 'text',
        'input'                      => 'multiselect',
        'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
        'user_defined'               => true,
        'required'                   => true,
        'visible'                    => true,
        'source'                     => 'ns/some_source', // this source has the "UK" value
        'backend'                    => 'eav/entity_attribute_backend_array',
        'default'                    => 'UK',
    )
);

我还尝试使用

使用“是/否”值
"type" => "boolean" 

"type" => "select",
'source' => 'eav/entity_attribute_source_boolean'

功能相同。

在所有情况下,使用default键正确填充eav_attribute表格列default_value。但是,对于“是/否”属性"default" => "1",就编辑页面上的输入而言,它不会执行任何操作。 “No”仍然被选中,我期待“是”,因为“1”被映射为“是”:

// Mage_Eav_Model_Entity_Attribute_Source_Boolean
$this->_options = array(
    array(
        'label' => Mage::helper('eav')->__('Yes'),
        'value' =>  1
    ),
    array(
        'label' => Mage::helper('eav')->__('No'),
        'value' =>  0
    ),
);

多选的情况也是如此:默认选择没有选项。

我没有想法。有没有人知道“默认”列/键的用途是什么,如果没有设置属性的默认值?如何设置要在新/编辑产品后端页面上自动选择的属性值?

1 个答案:

答案 0 :(得分:2)

我也遇到了这个问题,在创建自定义{product,customer,address}属性和添加实体时出现问题。

在Mage_Catalog_Model_Resource_Setup :: _ prepareValues中定义了一些“默认”实体集,这会导致这种麻烦。

最佳解决方案是在创建属性后加载属性并设置默认值。

 $model = Mage::getModel('eav/entity_attribute')
     ->load($installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'product_country'));
 $model
     ->setDefaultValue(Mage::helper('eav')->__('Yes'))
     ->save();
相关问题