How to save value for a Specific[custom] Product Attribute from Product Model ?

I have found the below script from here :

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

$product->setName('Some Random Name'); 

$product->getResource()->saveAttribute($product, 'name');
up vote 32 down vote accepted

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

  • Thanks for the reply But above script takes more time since its loads the model .Thanks for the reply But above script takes more time since its loads the model . – Roney Sep 19 '13 at 11:08
  • I've done an update of the code – Sander Mangel Sep 19 '13 at 11:40
  • Thanks for the effort taken – Roney Sep 20 '13 at 9:29
  • You're welcome. I actually already implemented in 2 import scripts and it saves a lot of time. – Sander Mangel Sep 20 '13 at 10:06
  • Small question: this was not the answer you were looking for? If not let me know, if so please close the question to keep our Answered question rate up :) – Sander Mangel Sep 20 '13 at 12:03

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

  • Thank you for the post and keeping it up-to-date. But i see a strange behavior. Like this save always gave me the best time. But lately in 2 Magento instances EE ones this too takes time to complete the Save. Could it be due to some extension on these instance. I can think of no other issue. These Magento instances are currently on by local dev system and am yet to transfer it to a test server instance. – zapping Apr 20 '16 at 10:19
  • @Fra you should run the same benchmark that Sander ran - would be a good comparison! – Robbie Averill Aug 9 '16 at 3:13

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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