Is there a way in Magento where I can assign Attribute X's value to Attribute Y programmatically ?

So far I have tried this. I created the Y attribute with following settings:

                   'label'=>'Value of Attribute X',

In my backend model, I have done this:

class Namespace_Module_Model_Entity_Attribute_Backend_Myattribute extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
    public function beforeSave($object){
        $attrCode = $this->getAttribute()->getAttributeCode();
        return $this;

I am able to see "HAHAHA" as the attribute value in the Product Edit page. I want to change this to value of another attribute. How do I do it ? How do I access another attribute's value of the same product from this class ?

PS: What I am actually trying to achieve is this. The attribute X is of type multi select with 100s of options. So the attribute Y must keep track of the options selected of X, and Y shows the value in product page in read-only format.


It's Simple



now assign the values to another attribute of that product



Don't forgot to save product.

  • Thanks but I do not quite get it. What is the value of $id in $prod=Mage::getModel('catalog/product')->load($id); ? – Hashid Feb 28 '14 at 17:40
  • product id mate. and you have to create the object of product – Keyul Shah Feb 28 '14 at 17:49

I have solved this finally. I went by a different approach, I used Observer. This is what I did:

I created an Observer with following code:

class Namespace_Module_Model_Observer

private $_processFlag; //to prevent infinite loop of event-catch situation
public function  copyAttribute($observer){


    $_store = $observer->getStoreId();
    $_product = $observer->getProduct();
    $_productid = $_product->getId();

    $attrA = $_product->getAttributeText('attributeA'); //get attribute A's value

    $action = Mage::getModel('catalog/resource_product_action');
    $action->updateAttributes(array($_productid), array('attributeB'=>$attrA),$_store); //assign attrA's value to attrB

And my config.xml went like this:


So basically, I am using the event catalog_product_save_after which is fired whenever a product is saved. In my observer, I catch the event, get attributeA's value and assign to attributeB, and finally save my product.

That's it! I don't know if this is the best method, but it does work!

PS: Thanks to Keyul Shah for helping me solve this. He helped me directly on facebook. However the answer he posted below doesn't seem to work.

  • you don't need $_product->save(); in the observer. It works without it. – Marius Jul 29 '15 at 6:05

A couple improvements to his answer that I came across while using it today. I can't comment due to no reputation so excuse me answering instead of commenting.


This was giving me a "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry" error upon creating a new product. This was fixed by changing to:


Also, I found that the module was only working when the attributeB already had a value. This was due to using updateAttributes. Since I was only editing one attribute, I changed:

$action->updateAttributes(array($_productid), array('attributeB'=>$attrA),$_store);


$_product->setData('attributeB', $attrA);

and now it works great! Hope this helps another noob like me, thanks guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.