PHP – How to Copy Product Attribute Values in Magento

htmlPHPproduct-attribute

i want to copy a product's attribute (weight) to another atribute (general_weight).

i found this codes, but i don't know what to do with this codes? or where to copy them?

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

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

assign the values to another attribute of that product with this code

    $prod->setAttreibuteCode($val);

    $prod->save();

i will thankful if you experts help me and explain by details.

** i want weight attribute value to be copied into general_weight always on product save.

Best Answer

Moh3n,i suggest that export an product from Admin in csv format. with sku,weight and then import that csv with change columns name to general_weight from weight.

pragmatically if want then follow below process:

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

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

        $prod->setGeneralWeight($val);

        $prod->save();

Update:

Create an script at magento root update.php and put the below code :

<?php
require_once  "app/Mage.php";
Mage::app("admin");
umask();

/*
 Step2: get Product Collection filter by Saleable
*/
$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('weight');

foreach($collection as $product){
$val=$product->getWeight();
/*$product->getWeight() is not given the value then used 

$product->getResource()->getAttribute('weight')->getFrontend()->getValue($product-)

*/
$product->setGeneralWeight($val);
$product->getResource()->saveAttribute($product, 'general_weight');

}

then run the script by browser or shell php program.

Related Topic