1

In my Magento 1.9.1 I am updating the inventory via script. Via this script I update almost 19K products. I have used $product=Mage::getModel('catalog/product')->load($id); to fetch the records and $product->save() to save the products records. But $product->save() is the slowest part of the script. Please let me what is the other method in Magento to fix this issue.

$stockItem = Mage::getModel('catalog/product')->load($id);
try {

    $stockItem->setStockData(array('is_in_stock' => $isinstock, 'qty' => $quantity));
    $weight_pro = ($length * $width * $height) / 166;
    if ($weight_pro < $weight) {
        $stockItem->setWeight($weight);
    } else {
        $stockItem->setWeight($weight_pro);
    }
    $stockItem->save();

} catch (Exception $e) {
    Mage::log('Failed for stockupdate  ' . $e);
}
  • maybe database is the bottleneck – stetoc Sep 7 '18 at 9:25
  • @stetoc what do you mean by bottleneck. I have monitor the mysql log there is no error. also no more CPU usage. sometimes its working sometimes its stop in between the execution. – Narayan Sep 7 '18 at 9:28
  • by bottleneck I mean that maybe db is just slow and that's why it's slowing down – stetoc Sep 7 '18 at 9:29
  • please add your full code here so that myself or others can help you in optimzation of code. – Pradeep Sanku Sep 7 '18 at 9:30
  • No @stetoc if I run by normal php query the execution is perfect and fast but whenever I used this product->save() script taking too much time and break in bettween the execution, – Narayan Sep 7 '18 at 9:32
1

Did you try saving with attribute instead of saving all the product ?

$product->setData('attribute','value');
$product->getResource()->saveAttribute($product, 'attribute');
0

if you save product then every time load product model and save product data so it will take to much time i sugest to run custom mysql query to update your weight.

For ex:

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

$weight_pro = ($length * $width * $height) / 166;
if ($weight_pro < $weight) {
    $query = "UPDATE m_catalog_product_entity_varchar SET value = '".$weight."' WHERE attribute_id = '89' AND entity_id = '".$ProductId."'";
    $updateStock = $writeConnection->query($query);
} else {
    $stockItem->setWeight($weight_pro);
    $query = "UPDATE m_catalog_product_entity_varchar SET value = '".$weight_pro."' WHERE attribute_id = '89' AND entity_id = '".$ProductId."'";
    $updateStock = $writeConnection->query($query);
}

89 is your product attribute id. when you edit arrtibute then you can show attribute is on url. https://prnt.sc/ks0twq

0

You must disable the indexation on save of product. Or you try the saveAttribute.

  • Its already set to "Manual Update". – Narayan Sep 10 '18 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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