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)

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
scroll top