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

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

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)