Updating 2500 product price using observer gives 500 internal server error

by Ankur Jain   Last Updated June 06, 2015 22:04 PM

I have created a module inside admin, which is based on Gold rates, if admin update the gold rates for specific Carat then it will update the prices for that Carat products.

Now my problem is that, if there are less no. of products like 400, it is working fine, but when there are around 2000 products it updates the few products and then gives the 500 internal server error.

I have written the code as per the guidelines mentioned in the URL:http://www.atwix.com/magento/working-with-large-collections/

Please Please suggest, and help me.



Answers 2


Seems like this error might be due to keep-alive setting or also the php max execution time, check your apache/nginx error log and php error log as well. You can change the php max execution time within your observer, altough I would not recomend it, at least try it with:

ini_set('max_input_time', 3600);

Also check apache or nginx keep alive.

And if it's possible to optimize your update script try to begin with that.

lloiacono
lloiacono
June 06, 2015 11:08 AM

    $write_connection = $this->_getConnection('core_write');            
                    $price_attribute_id = $this->_getAttributeId();
                    $attribute_table = $this->_getTableName('catalog_product_entity_decimal');

    $price_array = array("1002"=>12.00,"500"=>100.12); // price array corresponding to product id

$collection = Mage::getModel('product/catalog')->getCollection(); // Product collection object

// Below function is to iterate through collection faster than foreach
// passed some object like write connection, price attribute id so that we need not to create object everytime iterator will loop

Mage::getSingleton('core/resource_iterator')->walk(
                $collection->getSelect(),
                array(array($this, 'productCallback')),
                array('post' => $price_array,'write_connection'=>write_connection ,'price_attribute_id '=>$price_attribute_id ,'attribute_table'=>$attribute_table 
                      )
            );

function productCallback($args)
{
$product = Mage::getModel('catalog/product');
$product->setData($args['row']);
$data = $args['post'];
$product_id = $product->getId();


$write_connection = $args['write_connection'];

$price_attribute_id = $args['price_attribute_id'];
$attribute_table = $args['attribute_table '];

if($data[$product_id])
                        {
                            $sql = "UPDATE " .$attribute_table . " cped
                            SET  cped.value = '".$data[$product->getId()]."'
                            WHERE  cped.attribute_id = ".$price_attribute_id."
                            AND cped.entity_id = ".$product_id;

                         $write_connection->query($sql);


                        }                                                   
}
Ankur Jain
Ankur Jain
July 30, 2015 11:43 AM

Related Questions