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

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

报告相同问题?