douya5194 2014-10-16 12:46
浏览 38
已采纳

Magento:使用选项创建新属性

I am creating a simple import script for attributes and I cannot figure out how to add options to them. Every attribute of the 'Attribute' is straight forward, except for adding an option. Is this something that can be done upon the creation of an Attribute??

The code I use basically is below.

$model = Mage::getModel('catalog/resource_eav_attribute');  
$data = array(
    'is_global'                     => '0',
    'frontend_input'                => 'text',
    'default_value_text'            => '',
    'default_value_yesno'           => '0',
    'default_value_date'            => '',
    'default_value_textarea'        => '',
    'is_unique'                     => '0',
    'is_required'                   => '0',
    'frontend_class'                => '',
    'is_searchable'                 => '1',
    'is_visible_in_advanced_search' => '1',
    'is_comparable'                 => '1',
    'is_used_for_promo_rules'       => '0',
    'is_html_allowed_on_front'      => '1',
    'is_visible_on_front'           => '0',
    'used_in_product_listing'       => '0',
    'used_for_sort_by'              => '0',
    'is_configurable'               => '0',
    'is_filterable'                 => '0',
    'is_filterable_in_search'       => '0',
    'backend_type'                  => 'varchar',
    'default_value'                 => '',
    'frontend_label'                => '',
    'attribute_code'                => ''
);  
foreach ($header as $key => $value){
    if(isset($data[$key]) !== false){
        $data[$key] = $row[$header[$key]];
    }
}

$data['option'] = ?WHAT DO I DO HERE¿

$model->addData($data); 
$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());

$model->setIsUserDefined(1);
$model->save();

}

EDIT:

Thanks to Marko for his example, I tried the following;

$data['option'] = array (
        'value' => array(
                'wood' => array('Wood'),
                'metal' => array('Metal')
        )
);

His method for adding attributes in general is slightly different but the value for that attribute works just the same.

W00t!

  • 写回答

1条回答 默认 最新

  • dongmu5920 2014-10-16 12:57
    关注

    you can create sql script (tutorial: http://alanstorm.com/magento_setup_resources) and inside put something like:

    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    
    $setup->addAttribute('catalog_product', 'attr_code', array(
        'group'         => 'General',
        'input'         => 'select',
        'type'          => 'text',
        'label'         => 'Material',
        'backend'       => '',
        'visible'       => 1,
        'required'      => 0,
        'user_defined' => 1,
        'searchable' => 1,
        'filterable' => 0,
        'comparable'    => 1,
        'visible_on_front' => 1,
        'source' => 'eav/entity_attribute_source_table',
        'visible_in_advanced_search'  => 0,
        'is_html_allowed_on_front' => 0,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'configurable' => 1,
        'option'  => array (
                'value' => array(
                        'wood' => array('Wood'),
                        'metal' => array('Metal')
                )
        ),
    ));
    
    $installer->endSetup();
    

    this should create attribute Material with Wood and Metal options

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀