Skip to content

Commit

Permalink
Merge pull request #1 from danslo/preload-stock
Browse files Browse the repository at this point in the history
preload stock data
  • Loading branch information
brandung GmbH & Co. KG committed Jun 23, 2014
2 parents 5df2290 + 8e21d5d commit b6dffff
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions code/Model/Import/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ public function _importData()
return $result;
}

/**
* Preload stock data for products that we are importing.
* This prevents excessive amounts of stock_item model loads with large stock updates.
*
* @return Danslo_ApiImport_Model_Import_Entity_Product
*/
protected function _getStockItemData()
{
// Grab stock items for newSku.
$productIds = array_map(function($e) { return $e['entity_id']; }, $this->_newSku);
$stockItemCollection = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addFieldToFilter('product_id', array('in' => $productIds));

// Index them by product ID.
$stockItemData = array();
foreach ($stockItemCollection as $stockItem) {
$stockItemData[$stockItem['product_id']] = $stockItem;
}
return $stockItemData;
}

/**
* Stock item saving.
*
Expand Down Expand Up @@ -196,6 +218,9 @@ protected function _saveStockItem()
$defaultStockData['is_decimal_divided'] = 0;
}

// Do this in advance to prevent excessive loads.
$stockItemData = $this->_getStockItemData();

while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$stockData = array();

Expand All @@ -218,8 +243,11 @@ protected function _saveStockItem()
$row['stock_id'] = 1;

/** @var $stockItem Mage_CatalogInventory_Model_Stock_Item */
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($row['product_id']);
if (isset($stockItemData[$row['product_id']])) {
$stockItem = $stockItemData[$row['product_id']];
} else {
$stockItem = Mage::getModel('cataloginventory/stock_item');
}
$existStockData = $stockItem->getData();

$row = array_merge(
Expand Down

0 comments on commit b6dffff

Please sign in to comment.