Note: Having reviewed the code in the last 24 hours I suspect I can break this out into a specific function and use it in other parts of the ordering code so that it will stop duplication of email notifications as well as other problems. It's something I'm going to probably play with in the future so make sure you watch this thread.
Overall this just uses a physical file instead of the database to act as a flag to indicate whether the order has already had its inventory depleted or not. For some reason all attempts to use the database and existing flags in there failed to resolve the problem... I suspect code duplication but couldn't locate it (in over a year!) so this works and I'm using it...
Create a directory/folder called "inventory" in the /cache/ folder and make sure it has the same permissions as the cache folder
Open: /lib/orders.php
Find:
Code: Select all
function DecreaseInventoryFromOrder($orderId)
{
$order = GetOrder($orderId, false);
if (!$order) {
return false;
}
Code: Select all
// MOD Further attempts to stop multiple inventory updates
$invStatusDone = false;
$invLockFile = ISC_CACHE_DIRECTORY .'inventory/'.$orderId;
clearstatcache();
$invLastLock = file_exists($invLockFile) ? filemtime($invLockFile) : 0;
// Older than 60 seconds, update
if($invLastLock <= (time() - 60)) {
// Try to get lock
$fp = fopen(($invLockFile), "w+");
if(flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, time());
}
// Failed so something else got the lock and is updating inventory
else {
$invStatusDone = true;
}
// Unlock the file
@flock($fp, LOCK_UN);
// Close off the file handle
@fclose($fp);
}
else {
$invStatusDone = true;
}
/*
* If we can't get file lock or the inventory has already been updated in the
* last 60 seconds we return without updating the inventory further
*
* This SHOULD help avoid the inventory depletion duplication issue.
*/
if($invStatusDone === true) {
$GLOBALS['ISC_CLASS_LOG']->LogSystemDebug(
'php',
"Inventory status done or being resolved. Repeat depletion (avoided)",
"[File ".__FILE__." / Line".__LINE__."] Original code would have repeated inventory depletion <br />\nOrder: Order #{$order['orderid']}");
return true;
}
// MOD END Further attempts to stop multiple inventory updates
Payment: If you have been waiting, praying, begging for this fix then please make sure you donate to a suitable charity (Royal British Legion, Red Cross/Crescent, Cancer Research UK... whatever) by way of a pay it forward... This took two years to track down and nail so suitable karmic pass it on would be much appreciated. Thanks...
@Interspire... You can FREELY release this to your customers with PROPER attribution and recognition only.