I’m working on a PHP application integrated to shopify. Basically product information must be sync between the "shop" and application. Inside the products we have variants (similar to sub products). Shopify used to send webhooks with json data to report these changes. Every time I change/add/delete a variant, shopify send a "product update" webhook that changes only the json content. This is a example:
{
...
"variants": [{
"id": 279656846,
"position": 1,
"price": "80.00",
"product_id": 123022448,
"sku": "1000",
"option1": "30 cm",
"inventory_quantity": 10
},
{
"id": 291321287,
"position": 2,
"price": "15.00",
"product_id": 123022448,
"sku": "1003",
"option1": "15 cm",
"inventory_quantity": 23
}],
...
}
If I create a new variant it sends me a "product update" with current status, and has the new variant in json. Similarly, if I delete, it only send me a "product update" with current status, but without the deleted variant in json.
I created the following code that can treat properly the change/add case:
foreach ($jsonArr['variants'] as $rows) {
$variant = $rows['option1'];
$sku = $rows['sku'];
$salesPrice = $rows['price'];
$stockQty = $rows['inventory_quantity'];
$idVar = $rows['id'];
$dupchk = mysql_query("SELECT * FROM `variants` WHERE `idVar`='$idVar'",$con) or die (mysql_error());
$num_rows = mysql_num_rows($dupchk);
if ($num_rows > 0) {
$sql = "UPDATE `variants` SET `variant`='$variant',`sku`='$sku',`salesPrice`='$salesPrice',`stockQty`='$stockQty' WHERE `idVar`='$idVar'";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
}
else {
$sql = "INSERT INTO `variants`(`idVariant`, `idProduct`, `variant`, `sku`, `salesPrice`, `stockQty`, `comments`, `idVar`) VALUES ('','$idProduct','$variant','$sku','$salesPrice','$stockQty','','$idVar')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
}
}
The problem is that this code does not handle the delete variant case. I tried to do it but until now only create a "big confusing" code that could not work. Please, advise if you have any suggestion to a smart way to handle it.