Ad

How To Get Order_ID From Webhook Response - Shopify & Laravel

- 1 answer

I have registered my webhooks in my shopify app and i am able to receive responses from the webhook to my specified address.

But what i want to do now is, when i create an order and the response is sent to the specified address, how can i get the ID of the order created ?

I want to include the order id in the notification sent to the customer?

PS: I am new to laravel and creating apps in shopify.

Controller

  public function orderCreateWebhook(Request $request)
    {              
        //get order_id here and send notification
        notification = "A new order with id [order_id] has been 
                       created";
        SendSMS(notification); 
    }

   public function registerOrderCreateWebhook(Request $request)
    {
            $shop = "example.myshopify.com";
            $token = "xxxxxxxxxxxxxxxxxxxxxx";
            $shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
            Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' => 
             ['topic' => 'orders/create',
             'address' => 'https://shop.domain.com/order-create-webhook',
             'format' => 'json'
             ]
            ]);
    }

Web

//register order create
Route::get('/register-order-create-webhook', '[email protected]');  
Route::post('order-create-webhook', '[email protected]');
Ad

Answer

// get the content of request body    
$order = $request->getContent();

// decode json and covert to array
$order = json_decode($order, true);

// get the 'id' key which is your order id    
$order_id = $order['id'];
Ad
source: stackoverflow.com
Ad