Skip to content

Commit

Permalink
Addresses Incorrect stock versus sales. #364
Browse files Browse the repository at this point in the history
This is a possible solution to fixing the incorrect stock
when under high load, high concurrency on a single item
and slow payment gateways.
  • Loading branch information
Wilhelm Ellmann committed Nov 14, 2017
1 parent c760e36 commit 88c4e0a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/code/core/Mage/CatalogInventory/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public function reindexQuoteInventory($observer)
// Reindex previously remembered items
$productIds = array();
foreach ($this->_itemsForReindex as $item) {
$item->save();
$item->updateStockStatus();
$productIds[] = $item->getProductId();
}
Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds($productIds);
Expand Down
18 changes: 18 additions & 0 deletions app/code/core/Mage/CatalogInventory/Model/Resource/Stock/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,22 @@ protected function _prepareDataForTable(Varien_Object $object, $table)
}
return $data;
}

/**
* Update specific fields of a single row.
*
* @param int $itemId Item id of the row being updated
* @param mixed[] $data Array structure of fields being updated
*
* @return $this
*/
public function updateRecord($itemId, $data)
{
$adapter = $this->_getWriteAdapter();

$where = array('item_id = ?' => $itemId);

$adapter->update($this->getMainTable(), $data, $where);
return $this;
}
}
41 changes: 38 additions & 3 deletions app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function getQtyIncrements()
return $this->_qtyIncrements;
}

/**
/**
* Retrieve Default Quantity Increments data wrapper
*
* @deprecated since 1.7.0.0
Expand Down Expand Up @@ -556,8 +556,8 @@ public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)
$qty = intval($qty);

/**
* Adding stock data to quote item
*/
* Adding stock data to quote item
*/
$result->setItemQty($qty);

if (!is_numeric($qty)) {
Expand Down Expand Up @@ -767,6 +767,41 @@ protected function _beforeSave()
return $this;
}

/**
* Update Stock Status and low stock date as if $this->save() has been
* called, keeping the update query very light, by only touching those
* fields of interest
*
* @return $this
*/
public function updateStockStatus()
{
$this->setOrigData()->setDataChanges(false);
$this->_beforeSave();

if ($this->hasDataChanges()) {

$updatedFields = array(
'is_in_stock' => $this->getIsInStock(),
'stock_status_changed_auto' => $this->getStockStatusChangedAutomatically(),
'low_stock_date' => $this->getLowStockDate()
);

$this->getResource()
->updateRecord($this->getId(), $updatedFields);

Mage::dispatchEvent(
'cataloginventory_stock_item_save_after',
array(
'item' => $this,
'data_object' => $this,
)
);
}

return $this;
}

/**
* Chceck if item should be in stock or out of stock based on $qty param of existing item qty
*
Expand Down

0 comments on commit 88c4e0a

Please sign in to comment.