2011-09-01 137 views
2

我有一個名爲「vendor_id」的屬性。我有N個產品具有「vendor_id」作爲產品的屬性。當管理員添加新的「供應商」實體時,將以編程方式生成「vendor_id」屬性的選項。代碼是這樣的:以編程方式更新/編輯Magento中的屬性選項

public function saveAction() 
    { 
    $data = $this->getRequest()->getPost(); 
    $designerName = $data['title']; 

    $product = Mage::getModel('catalog/product'); 
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection') 
      ->setEntityTypeFilter($product->getResource()->getTypeId()) 
      ->addFieldToFilter('attribute_code', 'vendor_id') 
      ->load(false); 
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 
    $myresults = array ('value'=> array('optionone'=>array($designerName))); 

    $attribute->setData('option',$myresults); 
    $attribute->save(); 

而現在這個工作。它將爲「vendor_id」屬性創建一個選項,並且當用戶添加新產品(或編輯現有產品)時,「vendor_id」的下拉列表將由我們在saveAction上創建的這些「供應商」實體填充() 方法。

現在,在管理員想要編輯現有屬性選項的情況下,我不想創建新選項,我想編輯現有屬性。當我們更改名稱/標籤時,選項ID保持不變很重要。

我迷上在newAction設置靜態無功所以在saveAction(),我們可以檢查,看看我們編輯或創建一個新的選擇:

if (null == MyController::$_editScope) 
    { 
     error_log('Need to update option attribute'); 
     $attribute->addData($myresults); 
    } 

的問題是,addData ()方法就是這樣做的,它會添加數據,但不會更新現有的數據。屬性是:

$attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 

這是一個實例:http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html

其中有3倍的父類,我已經通過他們都找了,讓我來編輯方法*或*更新現有選項的名稱...

回答

3

你可以找到這個有用的。 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

如何爲特定商店視圖設置標籤? – Felix

+0

第一個鏈接死了,在[這裏]找到了相同的內容(http://arvind-07.blogspot.com/2012/05/addupdate-attribute-option-values.html) – Isaias

+0

@Isaias [That link](http: //www.webspeaks.in/2012/05/addupdate-attribute-option-values.html)現在可以使用。感謝您的通知。 –

相關問題