1

I am trying to have a cron to update a custom product attribute for configurable products,

public function updateReport() {
    Mage::app()->getStore()->setConfig('catalog/frontend/flat_catalog_product', 0);
    $configurableIds = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('type_id', 'configurable')->getAllIds();
    foreach ($configurableIds as $id) {     
        $product = Mage::getModel('catalog/product')->load($id); 
        $stock =0;
        $simpleProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
        foreach ($simpleProducts as $prod) {
            $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($prod);
            $stock += (0 + $qty->getQty());
        }
        if ($product->getSubProdQty() != $stock) {
            $product->setSubProdQty($stock)->save();
        }
    }
}

i have > 3000 configurable products my memory seems to run out after about 2000 products how can i fix this so my memory does not run out? (my memory is set to 512MB, this is magento 1.9.3.0)

1
  • Have you tried replaceing getUsedProducts() with something like getUsedProductsCollection()->getAllIds()? If you have many child products, this collection can be the leak's cause. Jul 10 '17 at 19:01
1

Don't save the product, but only the attribute. This can be done like:

$product->getResource()->saveAttribute($product, 'sub_prod_qty');

At the end of the loop unset the variables you are using. The GC should deal with them.

You can even avoid loading the product and you can even avoid loading the child products if you use SQL to sum up their qty and then fetch only the result.

1
  • the save is not what is killing the memory it runs out even if there is nothing to save, i have tried gc_collect_cycles and unset none are working Jul 10 '17 at 16:44

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.