0

I have script for the update price and QTY but it takes too much time on live server.I am trying to execute script for 4 to 5 hours but unable complete process. I have also tested script on my localhost it takes around 1 Hour and 30 Min to update 1400 products.

Please check my below script and suggest me idea so i can reduce timing for local and live both.

<?php

require_once 'app/Mage.php';
$app = Mage::app('default');
ini_set('max_execution_time', 0);
$currentStore = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
umask(0);

$path = ''.$_SERVER['DOCUMENT_ROOT'].'/product_stock_price.csv';
$readfile = file ($path);
$j=0;$k=0;
for ($i=1; $i < count($readfile); $i++ ) {
    $fields = split('"',$readfile[$i]);
    $sku=split(',',$fields[0]);
    $arr=array('sku'=>$sku[0],'stock'=>$fields[1],'price'=>$fields[3]);
    //print_r($arr);
    $sku= $arr['sku'];
    //print_r($sku);die;
    $product = Mage::getModel('catalog/product')->setStoreId(1)->loadByAttribute('sku',$sku);

    if ($product) 
    {
        //echo $product->getPrice(); 
        $product->setWebsiteId(1);
        $product->setStoreId(1);
        $product->setPrice($fields[3]);
        $product->save();

        $productId = $product->getId();
        $stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
        $stockItemId = $stockItem->getId();

        $stockItem->setData('manage_stock', 1);
        $stockItem->setData('qty', $fields[1]);

        $stockItem->save();

       // echo $SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",$fields[3],", Stock level: ",$fields[1];

        $updated++;
        $j+=1;
    }
    else
    {
        $k+=1;
        echo $sku." product not found.</br>";
    } 
}

echo "Total Row : ".count($readfile)." Proceed row : ".$i." Inserted Row : ".$j." Not Found : ".$k;


?>

Please give me some solution.

Thanks Jalpesh

Jalpesh Patel
  • 3,150
  • 10
  • 44
  • 68

1 Answers1

0

The process is too much heavy since it is running on big catalog. Instead of using model/collection, you can fire direct sql queries with minimal possible joins as per you need. You can easily create sql or just search for queries like to update qty is at Magento - update all products inventory with sql

Community
  • 1
  • 1
Deependra Singh
  • 1,504
  • 16
  • 31