我使用下拉列表来表示属性.不幸的是,我无法为单个产品设置这些属性的值.
我做了什么:
>创建属性集[PHP],
>为此set [PHP]添加了带有值的dropdown属性,
>在适当的属性集中添加新产品,并尝试为我创建的属性设置值.
private function setOrAddOptionAttribute($product,$arg_attribute,$arg_value) { $attribute_model = Mage::getModel('eav/entity_attribute'); $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table'); $attribute_code = $attribute_model->getIdByCode('catalog_product',$arg_attribute); $attribute = $attribute_model->load($attribute_code); $attribute_options_model->setAttribute($attribute); $options = $attribute_options_model->getAllOptions(false); // determine if this option exists $value_exists = false; foreach($options as $option) { if ($option['label'] == $arg_value) { $value_exists = true; break; } } // if this option does not exist,add it. if (!$value_exists) { $attribute->setData('option',array( 'value' => array( 'option' => array($arg_value,$arg_value) ) )); $attribute->save(); } $product->setData($arg_attribute,$arg_value); $product->save(); }
不幸的是它不起作用.有任何想法吗?我正在使用Magento 1.7.0.2