HI, I’ve spent far to long on this, so hoping someone might be able to help! (Thanks!)
When an order is placed, I want the stock status in my MySQL db to automatically update from ‘In Stock’ to ‘Sold’. So I’ve added a webhooks url in my dashboard and in the corresponding php file I have:
<?php
$json = file_get_contents('php://input');
$body = json_decode($json, true);
if (is_null($body) or !isset($body['eventName'])) {
// When something goes wrong, return an invalid status code
// such as 400 BadRequest.
header('HTTP/1.1 400 Bad Request');
return;
}
switch ($body['eventName']) {
case 'order.completed':
// This is an order:completed event
// do what needs to be done here:
// for each item in the JSON content.items array
foreach ($body['content']['items'] as $item) {
// get the item's id
$itemid = $item['id'];
$sql = "UPDATE posters SET stock_status='Sold' WHERE stock_num = $itemid";
mysqli_query($conn, $sql);
}
break;
}
// Return a valid status code such as 200 OK.
header('HTTP/1.1 200 OK');
?>
In my Add To Cart button code I have ’ data-item-id="<?php echo $row_posters['stock_num']; ?>"
and I can see in my failed examples that the stock_num in my db does end up okay in the JSON - content>item>id= (for example 504).
When I manually run a simple test version :
$sql = "UPDATE posters SET stock_status='Sold' WHERE stock_num = 504";
mysqli_query($conn, $sql);
It works fine.
If anyone could help it would be most appreciated. Thanks.