2

I've got a problem with a script which is syncing warehouse-system with e-commerce Magento.

Script is updating Magento's records directly to the database, price and quantity.

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 8309 bytes) in /lib/Zend/Cache/Backend/File.php on line 962

It's obviously memory-eating script, is there any way to decrease it's hungriness?:)

I can increase capacity of the memory but it won't be solution, as the moment there is about free 700MBs of memory.

There is about 1000 of lines in .csv file.

<?

define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';

umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen(MAGENTO . '/SECRET-FOLDER-WITH-CSV/quantity-and-prices.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {

    if ($count == 0) {
        foreach ($line as $key => $value) {
            $cols[$value] = $key;
        }
    }

    $count++;

    if ($count == 1)
        continue;

    #Convert the lines to cols 
    if ($count > 0) {
        foreach ($cols as $col => $value) {
            unset(${$col});
            ${$col} = $line[$value];
        }
    }

    // Check if SKU exists
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

    if ($product) {

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

        $newprice = $line[2];

        $product->setPrice($newprice)->save();


        if (!$stockItemId) {
            $stockItem->setData('product_id', $product->getId());
            $stockItem->setData('stock_id', 1);
        } else {
            $stock = $stockItem->getData();
        }

        foreach ($cols as $col => $value) {
            $stock[$col] = $line[$value];
        }

        foreach ($stock as $field => $value) {
            $stockItem->setData($field, $value ? $value : 0);
        }


        $stockItem->save();

        unset($stockItem);
        unset($product);
    }

    echo "<br />Stock updated $sku";
}
fclose($file);
0

3 Answers 3

3

Replace the Product's ->loadByAttribute for each loop by a single Product's collection load on which you'll iterate :

  • loop over your files lines and store each SKU you need
  • load a collection using this SKU array :

    $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('entity_id') //add what you need but restrict it the most possible ->addAttributeToSelect('sku', $skuArray) ->load()

  • iterate on your collection and do what you need

Sign up to request clarification or add additional context in comments.

Comments

3

Insert below Code

ini_set('memory_limit', '64M'); 

after

define('MAGENTO_ROOT', getcwd());

in index.php file. it is working with me.

Comments

2

Three different ways you can do:-

  1. edit ini.file - you described it, but it can be done only if hoster allowed to rewrite php settings in such manner.

  2. edit .htaccess file: php_value memory_limit 256M.

  3. edit index.php, add ini_set('memory_limit', '256M');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.