如何针对产品ID添加Products属性值。我想使用代码为特定产品ID添加属性值。我的产品代码属性是
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);
//Create an array to store the attribute data
$data = array();
//Create options array
$values = array(
//15 is the option_id of the option in 'eav_attribute_option_value' table
15 => array(
0 => 'Apple' //0 is current store id, Apple is the new label for the option
),
16 => array(
0 => 'HTC'
),
17 => array(
0 => 'Microsoft'
),
);
//Add the option values to the data
$data['option']['value'] = $values;
//Add data to our attribute model
$attr_model->addData($data);
//Save the updated model
try {
$attr_model->save();
$session = Mage::getSingleton('adminhtml/session');
$session->addSuccess(
Mage::helper('catalog')->__('The product attribute has been saved.'));
/**
* Clear translation cache because attribute labels are stored in translation
*/
Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
$session->setAttributeData(false);
return;
} catch (Exception $e) {
$session->addError($e->getMessage());
$session->setAttributeData($data);
return;
}
答案 0 :(得分:0)
在搜索之后,我找到了一些保存我的属性值的代码,而不是将它们存储在产品中。这是我的代码:
public static function getAttributeOptionId($arg_attribute, $arg_value,$id)
{
$id = $id;
//load the attribute by attribute code
$entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
$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);
$type = $attribute->getBackendType();
foreach($options as $option) {
if ($option['label'] == $arg_value) {
$arrk[] = $option['label'];
}
}
$optiont = self::getAttributeOptionValue($arg_attribute, $arg_value);
if(!$optiont){
$value['option'] = array($arg_value,$arg_value);
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
$product = Mage::getModel('catalog/product')->load($id);
$product->setMyAttribute($arg_attribute)->save();
}
return 0;
}
public function getAttributeOptionValue($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;
}.
其中$ arg_attribute是attriubte代码而$ arg_value是他的value.Id是特定sku的产品ID。我正在我的自定义模块中保存记录。如果有人知道此代码的问题,请通知我。谢谢