2013-02-27 59 views
0

如何根據產品ID添加產品屬性值。我想用code.My代碼以增加對特定產品ID屬性值的產品屬性是如何在magetno中針對產品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

嗨搜索後,我發現一些代碼,救我的屬性值,比我存儲他們對products.This是我的代碼:

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是現在的儲蓄在我的自定義記錄的產品ID爲特定sku.I模。誰知道這個代碼的問題,請通知我。謝謝

相關問題