Magento – How to Copy Product Attribute Values Programmatically

attributesbackendeavmagento-1.7product-attribute

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:

$setup->addAttribute('catalog_product','myAttribute',array(
                   'group'=>'General',
                   'input'=>'label',
                   'type'=>'varchar',
                   'label'=>'Value of Attribute X',
                   'visible'=>1,
                   'backend'=>'beta/entity_attribute_backend_myattribute',
                   'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE
               ));

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();
        $object->setData($attrCode,'HAHAHA');
        parent::beforeSave($object);
        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.

Best Answer

It's Simple

$prod=Mage::getModel('catalog/product')->load($id);

$val=$prod->getData('attribute_code');

now assign the values to another attribute of that product

$prod->setAttreibuteCode($val);

$prod->save();

Don't forgot to save product.