我有一個名爲「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倍的父類,我已經通過他們都找了,讓我來編輯方法*或*更新現有選項的名稱...
如何爲特定商店視圖設置標籤? – Felix
第一個鏈接死了,在[這裏]找到了相同的內容(http://arvind-07.blogspot.com/2012/05/addupdate-attribute-option-values.html) – Isaias
@Isaias [That link](http: //www.webspeaks.in/2012/05/addupdate-attribute-option-values.html)現在可以使用。感謝您的通知。 –