Trouble getting webhooks to update Add To Cart button after a sale

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:

$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');

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);
// 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.

Hi @fredquimby

Are you able to var_dump the $body to check its contents and check the appropriate parameter you need to attribute to $itemid?
You can also try to log $itemid or the full $sql query to understand what is going on.


Hi, thanks for that. Unfortunately for various reasons, I can’t. Can anyone say what their best guess is as to what might be the answer or what to try? I’m not being lazy - I’ve spent countless hours and hours on this - just have limited experience. Thanks.