2012-05-02 105 views
1

我想通過代碼(編程)更新/添加Magento中下拉屬性的選項。我已經找到了如何添加屬性選項,但我如何更新選項值。
例如:
假設屬性是'manufacturer'。我添加了三個選項man1,man2,man3。現在通過我的自定義代碼,我想將man1的標籤更改爲man11,將man2更改爲man22。我怎樣才能做到這一點? 謝謝。如何在Magento中以編程方式更新屬性選項?

回答

2

那麼,我自己找到了一個解決方案。 See complete details here

//Get the eav attribute model 
$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

您可能想要嘗試擴展位於app \ code \ core \ Mage \ Adminhtml \ controllers \ Catalog \ Product \ AttributeController.php的AttributeController並覆蓋saveAction()方法以滿足您的需求。

+0

在saveAction()中,需要爲該屬性設置所有配置。但是,我只想更新選項標籤而不是其他任何配置。 –

相關問題