Hi All,
Got a bit of a weird one I can't work out. I have a cart and when the update button is run, this script is run:
function diffInMins($startTime, $endTime) {
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$diffInSeconds = $endTime - $startTime;
$diffInMinutes = round($diffInSeconds / 60);
return $diffInMinutes;
}
function deleteAbandonedCart()
{
$yesterday = date('Y-m-d h:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
$sql = "SELECT * FROM cart";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$CartID = $row['id'];
$CartProductType = $row['productType'];
$CartProductID = $row['productID'];
$CartQty = $row['qty'];
$CartDate = $row['date'];
//Time left to buy
$DendTime = date('Y-m-d H:i:s');
$Dtimeleft = diffInMins($CartDate, $DendTime);
if ($CartProductType == "T") {
//Delete Ticket from cart if it's been there over an hour
if ($Dtimeleft > 30 || $CartDate < $yesterday) {
mysql_query("UPDATE tickets SET UnSold = UnSold + $CartQty WHERE id = $CartProductID");
mysql_query("DELETE FROM cart WHERE id = '$CartID'");
}
}
else {
mysql_query("UPDATE tickets SET UnSold = UnSold + $CartQty WHERE id = $CartProductID");
mysql_query("DELETE FROM cart WHERE id = '$CartID' AND date < '$yesterday'");
}
}
deleteAbandonedOrders();
}
What it's supposed to do is get rid of entries in the cart table after 30 minutes, but before that, replenish the stock (UnSold).
Trouble is, every now and then it looks like the stock doesn't get replenished, but does get removed from the cart table.
Can anyone see any issues that may be causing this?
I thought for some crazy reason it could be happening when the UnSold value is zero or somehow become NULL.... but UnSold = UnSold + $CartQty should still work shouldn't it?
Thanks for any help.