0

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

 $installer = $this;
        try{
            $productTypes = array(
                Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
                Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
             );
            $productTypes = join(',', $productTypes);

            $installer->startSetup();
        $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
        ));

      Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
      $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;
            $product->setDiscount($discountPer);
            $product->save();
          }
     }

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

2 Answers 2

1

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

1
  • 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
3

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()
    ->addAttributeToSelect('*')
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents();
foreach ($products as $product) {
    if($product->getFinalPrice() < $product->getPrice()){
        $diffAmt = $product->getPrice() - $product->getFinalPrice();
        $discountPer = ($diffAmt/ $product->getPrice()) * 100;
        Mage::getSingleton('catalog/product_action')->updateAttributes(
             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.

3
  • 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.