If we comment Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); then its work fine, but product update not reflected.

 $installer = $this;
            $productTypes = array(
            $productTypes = join(',', $productTypes);

        $installer->addAttribute('catalog_product', 'discount', array(
            'group'             => 'Prices',
            'type'              => Varien_Db_Ddl_Table::TYPE_VARCHAR,
            'backend'           => '',
            'frontend'          => '',
            'label'             => 'Discount',
            'input'             => 'price',
            'class'             => '',
            'source'            => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
            'visible'           => true,
            'required'          => false,
            'user_defined'      => true,
            'default'           => '',
            'searchable'        => false,
            'filterable'        => true,
            'comparable'        => false,
            //'visible_on_front'  => true,
            'unique'            => false,
            'apply_to'          => $productTypes,
            'is_configurable'   => false

      $products = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');

     foreach($products as $_product) {
            $product = Mage::getModel('catalog/product')->load($_product->getId());

          if($product->getFinalPrice() < $product->getPrice()){

            $diffAmt = $product->getPrice() - $product->getFinalPrice();
            $discountPer = ($diffAmt/ $product->getPrice()) * 100;

        catch(Exception $ex){
            echo $ex->getMessage();

2 Answers 2


Mage::getSingleton('catalog/product_action')->updateAttributes($array_product, array('attribute_code' => "something pretty"), 0);

$array_product => array of product ids, which could be easily fetched

using Mage::getModel("catalog/product")->getCollection()->getAllIds()

attribute_code => is your attribute code

something pretty => your attribute value

0 is store id replace with your store id

  • Thank you Keyur, Can you please read my comments at Marius answer ? I am facing that one issue. Commented Aug 5, 2015 at 7:02

The fact that you set the admin store in an install script has not effect. Read this for a clearer explanation: Current store is 1 when running upgrade scripts

But you can still use updateAttributes Something like this:

$products = Mage::getModel("catalog/product")->getCollection()
foreach ($products as $product) {
    if($product->getFinalPrice() < $product->getPrice()){
        $diffAmt = $product->getPrice() - $product->getFinalPrice();
        $discountPer = ($diffAmt/ $product->getPrice()) * 100;
             array($product->getId()), //products to update
             array('discount' => $discountPer), //attributes to update
             0 //store to update. 0 means global values

This way you avoid calling load in loops that is a real performance killer.

  • Hi Marius, Above code is useful to achieve what I want. This script is working fine If I execute it from external file. But still not working from mysql4-upgrade-0.1.0-0.1.1.php. I traced the code with all conditions too. It seems fine but update value of attribute is not working. Commented Aug 5, 2015 at 6:54
  • Strange. I used this and it worked. You can try to split your install script in 2. One that adds the attribute and one that does the update.
    – Marius
    Commented Aug 5, 2015 at 8:48
  • Marius, I have split code into 2 file. mysql4-install-0.1.0.php for create an attributes and mysql4-upgrade-0.1.0-0.1.1.php for update value to that attribute. But not updating value when you install it. Commented Aug 5, 2015 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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