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!

1 Like

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

1 Like

Thank you @yevhen, that did the trick and it is working well in testing. I was wondering if the hook also is activated when the booking gets cancelled? Or does that require a special snippet?

Yes, the code snippet should work also if a booking is canceled but it is recommended to do additional testing on your website

I tested it and the webhook did not receive data when cancelling the booking. I didn’t find anything peculiar in the logs, so what could be the cause and solution?

Also, what variable can I use to send the status of the booking among the data that is send to the webhook? So when the webhook receives data, it can be determined whether it is a confirmed booking or a cancelled one.

  1. Sorry for the inconvenience, but customization is beyond our support scope - it includes fixing bugs and guidance about the available features Support Policy | HivePress
    If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work https://fvrr.co/32e7LvY

  2. Please try to use $booking->get_status() to get booking status

Thank you @yevhen. I’m a bit confused by your reply and I’ll try to explain why:

  1. When I first considered HivePress, I contacted customer support and asked whether a feature was available to call a webhook. Customer support said yes and that I’d have to request a code snipped via the community.
  2. I did just that and received from you guys a code snippet. Which, with some additional testing, works.
  3. You mentioned somewhere in between, that if I needed advanced customization I would have to hire someone, which I understand, but all I needed was the code snippet to work.
  4. I ask whether the code snippet would also work when a booking gets cancelled, you say yes, but upon testing it doesn’t work. And now suddenly this is deemed as a type of customization beyond your scope of support. If something doesn’t work than it’s a bug right? Not a request for advanced customization?

It feels as if I’m missing some context as to why this falls out of the support scope.

Sorry for the confusion, I guess @yevhen made the initial snippet too specific so it appeared like we provide customizations as part of the support scope, when referring to the forum I meant a sample snippet to show how to hook custom functions with a sample webhook request using wp_remote_post to the booking status change event.

Unfortunately, we can’t implement specific business logic for your website and debug custom code (this is something that we would estimate as custom work depending on the time spent), we can only provide general guidance regarding the development, and we’re happy to suggest common snippets (we have a collection of 100+ snippets and we add more based on the forum topics) although customization is completely beyond the support scope by default.

If you have a developer for custom work, or you’re familiar with PHP basics please try staring with this code snippet:

add_action(
	'hivepress/v1/models/booking/update_status',
	function( $booking_id, $new_status, $old_status, $booking ) {

		// Check status.
		if ( 'publish' !== $new_status ) {
			return;
		}

		// Send request.
		wp_remote_post(
			'webhook-url-here.com',
			[
				'body' => [
					// add webhook parameters here,
					// e.g. booking details like $booking->get_something()
					// or parameters requred by webhook (like the API key)
				],
			]
		);
	},
	1000,
	4
);

It sends a webhook request when a booking is confirmed.

Ah I see. Well in that case I’m even more grateful for the extra support! And thank you both for all the service in this thread!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.