How to copy a product's attributes value to another attribute in Magento?
-
22-10-2019 - |
문제
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.
해결책 2
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){
if(!$this->_processFlag):
$this->_processFlag=true;
$_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
$_product->save();
endif;
}
And my config.xml went like this:
<events>
<catalog_product_save_after>
<observers>
<namespace_module>
<type>singleton</type>
<class>Namespace_Module_Model_Observer</class>
<method>copyAttribute</method>
</namespace_module>
</observers>
</catalog_product_save_after>
</events>
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.
다른 팁
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.
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.
$_product->save();
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:
$_product->getResource()->save($_product);
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);
to:
$_product->setData('attributeB', $attrA);
and now it works great! Hope this helps another noob like me, thanks guys!