How to save value for a Specific[custom] Product Attribute from Product Model
There are 2 ways to go about this, one is by getting the Magento catalog/product
model and loading the product by ID which will give you the whole product, then setting the name and saving it.
$product = Mage::getModel('catalog/product')->load(1);
$product->setName('foobar!');
try {
$product->save();
} catch(Exception $e) {
echo "{$e}";
}
As OP pointed out, this is quite heavy just for changing one attribute. I kind of figured that the attribute mass update tool should use a cleaner way to do this and found the Mage_Catalog_Model_Resource_Product_Action
class
$product_id = 1;
$store_id = 0;
$action = Mage::getModel('catalog/resource_product_action');
$action->updateAttributes(array($product_id), array(
'name' => 'foobar!'
), $store_id);
[UPDATE] benchmark
So did a quick benchmark script and the results speak for itself.
$starttime = microtime(true);
for ($i=20; $i>0; $i--)
{
$action = Mage::getModel('catalog/resource_product_action');
$action->updateAttributes(array(1), array(
'name' => 'foobar!'
), 0);
}
echo "Time: " . (microtime(true) - $starttime) . " seconds\n";
$starttime = microtime(true);
for ($i=20; $i>0; $i--)
{
$product = Mage::getModel('catalog/product')->load(1);
$product->setName('foobar!');
$product->save();
unset($product);
}
echo "Time: " . (microtime(true) - $starttime) . " seconds\n";
Time: 0.076527833938599 seconds
Time: 4.757472038269 seconds
If you need to save just one attribute and you already have the product loaded you can use also this method:
$product->setData('attribute_code',$someData);
$product->getResource()->saveAttribute($product,'attribute_code');
This method is much faster than catalog/resource_product_action