2014-10-16 136 views
0

我正在爲屬性創建一個簡單的導入腳本,我無法弄清楚如何向它們添加選項。除了添加選項外,「屬性」的每個屬性都很簡單。這是可以通過創建屬性來完成的嗎?Magento:使用選項創建新屬性

我使用的代碼基本如下。

$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(); 

}

編輯:

感謝馬爾科他的例子,我嘗試以下;

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

他的添加屬性的方法通常略有不同,但該屬性的值工作原理相同。

W00t!

回答

1

您可以創建SQL腳本(教程:http://alanstorm.com/magento_setup_resources)和內放像:

$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(); 

這應該創建屬性材料,木材和金屬備選

+0

我寫一個CLI腳本,什麼情況下是$這個變量? – user3661841 2014-10-16 13:06:52

+0

$這個變量實際上是類Digramm_Productvariants_Model_Resource_Mysql4_Setup的實例。這是由Alan Storm在我上面發佈的教程中解釋的。 – 2014-10-16 13:10:06