Webhook product update

java2dev
New Member
14 0 0

When a product is updated using a webhook I receive the whole attribute information in json, there's a way to know whish attribute has been update, because I need to send it using an external API so I can`t send all informations products after the product has been created.

Replies 8 (8)

Kristina
Shopify Staff (Retired)
46 0 4

Hi,

Unfortunately, it's not possible to determine which attribute was updated from the returned JSON.
However, if you store the product's data each time you use a webhook, you can compare this to the data received the next time you call a webhook.

Hope this helps,

Kristina

To learn more visit the Shopify Help Center or the Community Blog.

java2dev
New Member
14 0 0

Thanks for you answer chris, the idea to compare it sounds good but is not optiomal for example for a huge call between shopify and an external API.

I have a doubt how can I get the id product from the webhook product update response?

Thanks.

Kristina
Shopify Staff (Retired)
46 0 4

Yes, it wouldn't be particularly great for high volume calls, unfortunately. 

Are you also hoping to get the ID for the product you've just updated?
When you make a request to 

PUT /admin/products/#{id}.json

the response includes the product ID - like this:

HTTP/1.1 200 OK
{
  "product": {
    "id": 632910392,
    "title": "New product title",
    ...
    ... etc


You can take a look at the Update API Request docs here.  

Is that the product ID you're trying to grab?

Kristina

To learn more visit the Shopify Help Center or the Community Blog.

java2dev
New Member
14 0 0
webhookContent = "";

$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
    $webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$webhookContent = json_decode($webhookContent, true);
$id = $webhookContent['id'];
file_put_contents('webhook.xml', $id); // The id is printed in the file
$pr = new Product($id, $category, $credential, $shop); // at this level the $id is empty

Hi Cris, yes I try to print in a file the call so I use the code at top.

I don't know if I need to use $_GET or $_REQUEST to save the id value in a variable and then use it in "new product"

Thanks in advance.

Kristina
Shopify Staff (Retired)
46 0 4

If you're hoping to update a product and then grab the ID returned from that updated product, you'd use PUT:

PUT /admin/products/#{id}.json

However, if you just want to grab the ID from an existing product and store it in a variable, you can return the same product object with GET:

GET /admin/products/#{id}.json?fields=id

which will only return the ID field and nothing else.

I typically grab requests using cURL with php. Something like this:

// create a new cURL resource
$ch = curl_init();

// set your URL to the request URL
curl_setopt($ch, CURLOPT_URL, "http://{shop}.myshopify.com/admin/products/#{id}.json");
curl_setopt($ch, CURLOPT_HEADER, 0);

// pass to the browser to return results of the request
$result = curl_exec($ch);

// closes cURL resource
curl_close($ch);

// all product information now exists in the $result variable 
// as JSON; you can use that with the JSON.decode to convert the 
// string to a PHP object (or array if you pass the 'true' optional 
// parameter

$product = JSON.decode($result)

// Then you can work with this $product variable as you
// would any other PHP array or object for presenting your data


What does 

php://input

represent in your code example?

To learn more visit the Shopify Help Center or the Community Blog.

java2dev
New Member
14 0 0
php://input

is to return the data received after updating a product. The idea I've found in this post.

so using only the variable $webhookContent in the output I print in the file this

{"id":8909989260,"title":"bicicleta","body_html":"\u003cp\u003etodo de reloj\u003c\/p\u003e","vendor":"Rolex","product_type":"Relojes","created_at":"2016-11-14T17:42:28-05:00","handle":"bicicleta","updated_at":"2016-11-28T12:50:22-05:00","published_at":"2016-11-16T22:10:00-05:00","template_suffix":null,"published_scope":"global","tags":"","variants":[{"id":30781726924,"product_id":8909989260,"title":"Default Title","price":"83510.00","sku":"12345678gh","position":1,"grams":1000,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":null,"option1":"Default Title","option2":null,"option3":null,"created_at":"2016-11-14T17:42:28-05:00","updated_at":"2016-11-28T12:50:22-05:00","taxable":true,"barcode":"7037838585863837","image_id":null,"inventory_quantity":1,"weight":1.0,"weight_unit":"kg","old_inventory_quantity":1,"requires_shipping":true}],"options":[{"id":10760107980,"product_id":8909989260,"name":"Title","position":1,"values":["Default Title"]}],"images":[],"image":null}

For curl I have it yet in an API class to send data where I can create and synchronize content and image data with external API.

So my idea for the update , i.e I change the price so a json (as above) is generated after the event product/update , I search to use the id product json to send my XML to the external API. when I update using a static id into the product object

$pr = new Product(8909989260, $category, $credential, $shop);

and then Post the page.php the update is applicated. So the unic issue I've found is to get the product id updated to use it into $pr because after that I have all actions ready to send the data to the external API. I hope that I explained well the issue.

Kristina
Shopify Staff (Retired)
46 0 4

I may be misunderstanding, but are you trying to grab (in this example), 8909989260? The product ID of returned from the request?

If so, when you decode the JSON with 

JSON.decode($result)

you should be able to access the product ID directly and pass that to:

$pr = new Product(8909989260, $category, $credential, $shop);

To learn more visit the Shopify Help Center or the Community Blog.

java2dev
New Member
14 0 0

Hi, chris, yes it's working , thank you.