`
cgzhang
  • 浏览: 74262 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Magento中添加带有选项的属性

PHP 
阅读更多

下面的代码为Customer实体添加了性别属性,有两个可选值 Male和Female

$installer->startSetup();

$installer->addAttribute('customer', 'gender', array(
    'label'        => 'Gender',
    'visible'      => true,
    'required'     => false,
    'type'         => 'int',
    'input'        => 'select',
    'source'        => 'eav/entity_attribute_source_table',
));


$tableOptions        = $installer->getTable('eav_attribute_option');
$tableOptionValues   = $installer->getTable('eav_attribute_option_value');

// add options for level of politeness
$attributeId = (int)$installer->getAttribute('customer', 'gender', 'attribute_id');
foreach (array('Male', 'Female') as $sortOrder => $label) {

    // add option
    $data = array(
        'attribute_id' => $attributeId,
        'sort_order'   => $sortOrder,
    );
    $installer->getConnection()->insert($tableOptions, $data);

    // add option label
    $optionId = (int)$installer->getConnection()->lastInsertId($tableOptions, 'option_id');
    $data = array(
        'option_id' => $optionId,
        'store_id'  => 0,
        'value'     => $label,
    );
    $installer->getConnection()->insert($tableOptionValues, $data);

}

$installer->endSetup();

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics