我创建了一个非常大的导入脚本,它将产品从CSV导入到magento.我还有一个要解决的问题.
我使用下拉列表来表示属性.不幸的是,我无法为单个产品设置这些属性的值.
我做了什么:
>创建属性集[PHP],
>为此set [PHP]添加了带有值的dropdown属性,
>在适当的属性集中添加新产品,并尝试为我创建的属性设置值.
我尝试了几种方法,这里的one对我来说很好看:
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
你可以这样做:
$attr_id = $this->attributeValueExists('manufacturer','Samsung');
$product->setData('manufacturer',$attr_id );
$product->save();
public function attributeValueExists($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_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}
return false;
}
如果您有任何疑问,请告诉我.
热忱.