开发者

Programmatically updating/editing an attribute's option in Magento

开发者 https://www.devze.com 2023-04-01 03:32 出处:网络
I have an attribute called \"vendor_id\". I have N products that have the \"vendor_id\" as an attribute of the product. The options for the \"vendor_id\" attribute are being generated programmatically

I have an attribute called "vendor_id". I have N products that have the "vendor_id" as an attribute of the product. The options for the "vendor_id" attribute are being generated programmatically when an admin adds a new "vendor" entity. The code is like this:

    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(); 

And this works for now. It will create an option for the "vendor_id" attribute, and when the user adds a new product (or edit's an existing product) the drop down for "vendor_id" will be populated by these "vendor" entities we've created on the saveAction() method.

Now, in the case an admin wants to edit an existing attribute option, I don't want to create a new option, I want to edit an existing one. It's important that the option ID stays the same when we change the name/label.

I've hooked in the newAction to set a static var so in the saveAction() we can check and see if we are editing or creating a new option:

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

The problem is that the addData() method does just that, it adds the data, but doesn't update the existing on开发者_开发问答. The attribute is:

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

Which is an instance of: http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html

Which has 3x parent classes and I've looked through them all for a method that will allow me to edit* or update* an existing option's name ...


You can find this useful. 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号