Getting Order Details from webhook.

raff25c
New Member
6 0 0

Hi! Im having a problem with the webhook content for new order. Am i doing it right? What do you think is the problem with my code? it only insert order id and created at and updated at to my db table. All other columns are blank. Thanks!

		$webhook_content = '';
		$webhook = fopen('php://input' , 'rb');
		while(!feof($webhook)){ //loop through the input stream while the end of file is not reached
			$webhook_content .= fread($webhook, 4096); //append the content on the current iteration
		}
		fclose($webhook); //close the resource
		$orders = json_decode($webhook_content, true); //convert the json to array
		
		//Save to text file for checking
		echo file_put_contents("test.txt",$webhook_content);
		
		foreach($orders as $order){ 
		
                $order_id = $order['id'];
                $order_number = $order['name'];
                $f_name = $order['billing_address'][0]['first_name'] . ' ' . $order['billing_address'][0]['last_name'];
                $payment_gateway = $order['gateway'];
                $financial_status = $order['financial_status'];
                $order_value = $order['total_price'];
                $order_status = $order['#'];
                $shipping_province = $order['shipping_address'][0]['province'];
                $created_at = $order['created_at'];
                $updated_at = $order['updated_at'];
                $shipping_method = $order['shipping_lines'][0]['title'];
				
		}
				$conn = connect_db();
				
				$sql = "INSERT INTO orders(order_id, order_number, cust_fname, payment_gateway, financial_status, order_value, order_status, ship_to, created_at, updated_at, shipping_method)
                                    VALUES ('" . $order_id . "', '" . $order_number . "', '" . $f_name . "', '" . $payment_gateway . "', '" . $financial_status . "', '" . $order_value . "', '" . $order_status . "', '" .$shipping_province . "', '" . $created_at . "', '" . $updated_at . "', '" .$shipping_method . "')";

				if ($conn->query($sql) === TRUE) {
					//echo "success";
				} else {
					echo "Error: " . $sql . "<br>" . $conn->error . "<br>";
					echo "DB Stat:" . $db_status . "<br>";

				}

				$conn->close();
banned
Reply 1 (1)

Visely-Team
Shopify Partner
1843 210 488

When registering a webhook with Shopify, there is a parameter called "fields" that you can use to list the fields you want Shopify to send your with the payload. Make sure it's not too restrictive.

Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog