Code snippet to call webhook on the booking status change

I was told I could request a code snippet here for the following situation:

I need to call a webhook once the booking gets confirmed. I would need to send:

  • The time and date of the booking
  • Booking duration
  • User ID of the client (who booked)
  • The user ID of the consultant (who got booked).
  • And if possible, send something to identify the booking. Such as ordernumber.

Thank you in advance for your service!

Please try this PHP snippet but please note that it can require further customization

add_action(
	'hivepress/v1/models/booking/update_status',
	function( $booking_id, $value ) {
		if('publish' === $value){
			$booking = \HivePress\Models\Booking::query()->get_by_id($booking_id);
			
			if(!$booking){
				return;
			}
			
			$listing = $booking->get_listing();
			$vendor = null;
			
			if($listing){
				$vendor = $listing->get_vendor__id();
			}
			
			$order = hivepress()->helper->get_first_array_value(
				wc_get_orders(
					[
						'limit'      => 1,
						'meta_key'   => 'hp_booking',
						'meta_value' => $booking_id,
					]
				)
			);
			$order_id = null;
			
			if($order){
				$order_id = $order->get_id();
			}
			
			$data = [
				'booking_date_created' => $booking->get_created_date(),
				'booking_duration' => round(($booking->get_end_time() - $booking->get_start_time()) / (60 * 60 * 24)),
				'booking_client' => $booking->get_user__id(),
				'booking_consultant' => $vendor,
				'booking_order' => $order,
			];
			
			wp_remote_post(
				'example.com',
				['body' => $data],
			);
		}
	},
	1000,
	2
);

Hi Yevhen,
Thank you for your reply. I have tested it, but it seems that the $data is not send in the body.

[
{
"headers": {
"host": "[redacted]",
"user-agent": "WordPress/6.0.2; https://[redacted]",
"content-length": "0",
"accept": "*/*",
"accept-encoding": "deflate, gzip, br",
"content-type": "application/x-www-form-urlencoded",
"referer": "[redacted]",
"x-forwarded-for": "[redacted]",
"x-forwarded-host": "[redacted]",
"x-forwarded-port": "443",
"x-forwarded-proto": "https",
"x-forwarded-server": "[redacted]",
"x-real-ip": "[redacted]"
},
"params": {
},
"query": {
},
"body": {
}
}
] 

I inserted the snippet with a plugin (WPCode) and the first test, as I mentioned, succesfully called the webhook but with no data.

I tried changing the format of the array and tried several things. But everytime the webhook did not receive the call anymore. Last one tried was with the following adjustment:

			wp_remote_post(
				'[webhook-url]',
				array(
					'body' => 'test'
				)
			);

Then I checked the server error log and saw this:

PHP Fatal error: Uncaught Error: Class "HivePressModelsBooking" not found in /usr/local/lsws/wordpress/wp-content/plugins/insert-headers-and-footers/includes/class-wpcode-snippet-execute.php(260) : eval()'d code:5 

I changed the snippet above, please check it again and make sure that it’s copied exactly.

Calling webhook was successful and this is the content of body:

	{
  "booking_date_created": "2022-09-23 13:24:29",
  "booking_duration": "0",
  "booking_client": "3",
  "booking_consultant": "105"
}

A few notes:

  • No ordernumber was send with it.
  • Booking duration is not correct. But upon looking at the code, I realise, if it’s possible to just get the end time, I don’t need the duration. Just the begin and end time of the booking.
  • And with booking date I meant the date of the appointment.
  • The reason I need this, is so I can schedule a videomeeting, integrating with AtomChat. AtomChat syncs with the wordpress users to know by userID which account should be joined for the converstation between client and consultant. And since Vendor is always connected to a Wordpress User, I’d need the Userid of the user behind the vendor instead of the vendor id.
    Would that be possible?

Yes, you can get user ID this way:

$listing->get_user__id();

Passing specific details depends on your requirements, if you’re familiar with PHP basics please try getting the booking details this way:

$booking->get_start_time();
$booking->get_end_time();

The code that gets a linked order seems to be correct, please try checking this via error_log or var_dump. HivePress sets the booking ID in the hp_booking order meta.

With the following adjustment:

			$data = [
				'booking_start_time' => $booking->get_start_time(),
				'booking_end_time' => $booking->get_end_time(),
				'booking_client' => $booking->get_user__id(),
				'booking_consultant' => $listing->get_user__id(),
				'booking_order' => $order,
			];

This is the result I got:

{
  "booking_start_time": "1664188200",
  "booking_end_time": "1664190000",
  "booking_client": "3",
  "booking_consultant": "1"
}
  • I still don’t receive ordernumber. I don’t see anything in the error log. How to check var_dump?
  • How to interpret the values of start time and end time to actual time?
  • Is there a code for the date of the booked appointment? $booking->get_start_date(); for instance?
  1. Please try to use $order_id instead of $order. Here is detailed information on how to use var_dump PHP: var_dump - Manual
  2. Please try to use the date function PHP: date - Manual to get the date in the standard format
    For example date('Y-m-d', $booking->get_start_time());
  3. If you mean getting the date when the booking was made then please try to use $booking->get_created_date()

I hope this guide will be helpful for you but please note that it can require advanced customization which can require hiring someone for custom work https://fwd.cx/hLhc73mQCD9R