简体   繁体   中英

How To Get ID of Order Deleted from Shopify Webhook - Laravel & Shopify

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM