Ad

How To Get ID Of Order Deleted From Shopify Webhook - Laravel & Shopify

- 1 answer

In my tutorial, I realised that I am able to get the number/id of the order during every event except orders/delete. In my controller below, I try to retrieve the order number just as I do for every topic ('orders/create', 'orders/paid')etc, but then I get an error saying:

Undefined index: number in Controller

Controller

  public function registerOrderDeleteWebhook()
    {
             $shop = Auth::user()->site;
            $token = Auth::user()->access_token;
            $shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
            Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' => 
             ['topic' => 'orders/delete',
             'address' => 'https://example.domain.com/order-delete-webhook',
             'format' => 'json'
             ]
            ]);
    }



public function orderDeleteWebhook(Request $request)
 {
        $order = $request->getContent();
        $order = json_decode($order, true);
        $order_id = $order['number'];

        //send notification to Admin with order number deleted below        

 }

Why could this be happening for only orders/delete?

Ad

Answer

the undefinded index error

occurs because there is no number field in Shopify Order Delete webhook response. Moreover, it is always a good idea to check if the field exists in the first place.

If you look at the Delete Order response sent by Shopify, it only includes,

{
  "id": 777859760246
}

where id is the Order Id. But as the order is deleted you cannot get anymore details later even via API. According to this forum post an order cannot be deleted until it is cancelled first. So a workaround is to listen to Order Cancel hook too and save this information somwehere in your Laravel application ( database etc ) and use it later when Order Delete webhook is recieved.

Ad
source: stackoverflow.com
Ad