0

I am going around in circles trying to figure out how to consume a webhook initiated by Shopify, within my plugin. I have the webhook setup on the Shopify and can verify that it's calling the right URL. But I am unsure how I can access the data passed in the POST. I have cut this down to the absolute basics and what I am seeing just doesn't make any sense to me.

The following code is registered with for add_action(init,....) - just outputting to the error_log to try to figure this out. It is being successfully invoked when Shopify invokes the webhook. But I am confused what it's telling me.

error_log('1: -----------------------------------------------');
  foreach($_POST as $key => $value)
{
  error_log($key . ':' . $value);
}

error_log('2: -----------------------------------------------');

Here is the log I get (I don't know why the function is invoked four times, but that's another story).

1: -----------------------------------------------
2: -----------------------------------------------
1: -----------------------------------------------
2: -----------------------------------------------
1: -----------------------------------------------
action:notifications_heartbeart
_ajax_nonce:64801213c5
post_id:
2: -----------------------------------------------

How do I access the actual payload? Here's what I should apparently be receiving, as per Shopify docs.

{
  "id": 706405506930370084,
  "email": "bob@biller.com",
  "accepts_marketing": true,
  "created_at": null,
  "updated_at": null,
  "first_name": "Bob",
  "last_name": "Biller",
  "orders_count": 0,
  "state": "disabled",
  "total_spent": "0.00",
  "last_order_id": null,
  "note": "This customer loves ice cream",
  "verified_email": true,
  "multipass_identifier": null,
  "tax_exempt": false,
  "phone": null,
  "tags": "",
  "last_order_name": null,
  "addresses": [
  ]
}
  • the init action is thrown at every page load then what you see in the log is a normal load and there is no trace of the webhook. which URL is called by the weebhook ? – mmm Apr 28 '18 at 18:22
  • Thank for the response, @mmm. The URL Shopify calls is <mydomain>.com/?action=shopify-add. There is no other traffic on this dev server and when I invoke the webhook from Shopify I see these four calls to my function repeated. So I know the call is coming in OK and it's reaching my function but no matter what I look at in the $_POST I don't see anything that resembles what Shopify indicates should be passed. Thanks again. – Mark Williams Apr 28 '18 at 18:29
  • then to catch the call in the init hook, you can test if (isset($_GET["action"]) && ("shopify-add" === $_GET["action"])) {... – mmm Apr 28 '18 at 18:39
  • Thank you again. That is actually where I started and that works fine (the code you see if after I have narrowed this down to the bare bones but when I look at $_POST['action'] I see what I expect). But the challenge I have is that I am trying to access what is sent as the payload, rather than just in the URL. Shopify, for this particular webhook, passes a fair bit of information about the customer (see bottom of opening post) - but I have no idea how to access that :( Thank you again. – Mark Williams Apr 28 '18 at 18:43
  • It's probably sending the content in the request body. See this question on Stack Overflow for how to access it. – Jacob Peattie Apr 29 '18 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.