4
votes

I am trying to programatically update a product's price within a custom module. This is my code:

Mage::setIsDeveloperMode(true); // for debug only
try
{
    $product = Mage::getModel('catalog/product')->load($productId);
    $product->setPrice($newPrice);
    $product->save();
}
catch (Exception $ex)
{
    echo "Error: ". $ex->getMessage();
}

When this code executes, I get the following exception:

Warning: Invalid argument supplied for foreach() in /home/www-data/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1180

In the system.log file, I see allot of these entries:

2016-03-17T18:01:06+00:00 ERR (3): Warning: Invalid argument supplied for foreach() in /home/www-data/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1180 2016-03-17T18:01:06+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be of the type array, null given, called in /home/www-data/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defined in /home/www-data/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php on line 543 2016-03-17T18:01:06+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Eav_Model_Entity_Abstract::_canUpdateAttribute() must be of the type array, null given, called in /home/www-data/public_html/app/code/core/Mage/Catalog/Model/Resource/Abstract.php on line 545 and defined in /home/www-data/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1254 2016-03-17T18:01:06+00:00 ERR (3): Warning: array_key_exists() expects parameter 2 to be array, null given in /home/www-data/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1256

Any idea what might be causing this? The same code on my dev magento setup works, but in production magento, this error started to appear, so I am a little confused.

4
must be of type array, null given... so there you go. you passed a null to something expecting an array instead.Marc B
@MarcB already check $newPrice it's not null and not meant to be an array either. I am perplexed by the error, because it's so random.Latheesan

4 Answers

23
votes

You can to try set the store:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

1
votes
$storeId='store_id';

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$productIds = array('products_ids');

$products = Mage::getModel('catalog/product')->getCollection();
$products->addStoreFilter();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('entity_id', array('in' => $productIds));
foreach($products as $product)
{
    $price=$product->setPrice('price');
    Mage::app()->setCurrentStore($storeId);
    $product->save(); 
}

Try this code, It works for me.

1
votes

Try below code. Need to pass store Id. This seems to be the issue.

$array_product = array($productId);
Mage::getSingleton('catalog/product_action')->updateAttributes($array_product, array('price' => $newPrice), $storeId);
-1
votes

Please make a note if you update product programmatically then try to avoid magento save function which is perfect for single product but when you will use lots of product it will make your site slow. Please use my below code then rendex magento over SSH . it will work fine for you.

$write = Mage::getSingleton('core/resource')->getConnection('core_write');


$write->query("
                      UPDATE catalog_product_entity_decimal val
                      SET  val.value = (".$productPrice['Price'].")
                      WHERE  val.attribute_id = (
                         SELECT attribute_id FROM eav_attribute eav
                         WHERE eav.entity_type_id = 4 
                           AND eav.attribute_code = 'price'
                        )
                        AND val.entity_id = '".$product->getId()."'
                    ");