0
votes

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?

1
Did you tried to dump your $request and $order variables? Can you put the portion of code where orderDeleteWebhook function is called?gomd
@gomd, the portion code is already there.. When a user deletes an order on shopify, the post request is sent to the address https://example.domain.com/order-delete-webhook which triggers orderDeleteWebhookMichael Anaman
Ok, then, can you try to dump your $request and $order variables with var_dump function? In order to see if the problem is in how you make the request or how you interpret the request.gomd

1 Answers

3
votes

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.