Form-Specific Event Title & Description

Use different Google Calendar event titles and/or descriptions depending on which form created the booking. This is useful when the same service is shared across several booking forms and each form needs its own merge tags (which vary by field ID from form to form).

Event settings are configured on the service, so the same template applies to every form that books that service. Each rule below targets a specific service + form pair and overrides the template for that combination. Templates are processed with the booking’s own form and entry, so form-specific merge tags (e.g. {:3}) resolve correctly.

Instructions

  1. Install this snippet by following the steps here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

  2. Update the $rules below with a row per service + form. Omit ‘title’ or ‘description’ to leave that value as configured in the service’s event settings.

Code

Filename: gpb-form-specific-event-title-description.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 * Gravity Perks // Bookings // Form-Specific Event Title & Description
 * https://gravitywiz.com/documentation/gravity-forms-bookings/
 *
 * Use different Google Calendar event titles and/or descriptions depending on which form
 * created the booking. This is useful when the same service is shared across several booking
 * forms and each form needs its own merge tags (which vary by field ID from form to form).
 *
 * Event settings are configured on the service, so the same template applies to every form
 * that books that service. Each rule below targets a specific service + form pair and overrides
 * the template for that combination. Templates are processed with the booking's own form and
 * entry, so form-specific merge tags (e.g. {:3}) resolve correctly.
 *
 * Instructions:
 *
 * 1. Install this snippet by following the steps here:
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Update the $rules below with a row per service + form. Omit 'title' or 'description' to
 *    leave that value as configured in the service's event settings.
 */
add_filter( 'gpb_google_calendar_event_data', function( $event_data, $booking ) {

	// Each rule targets a service_id + form_id. Values support GPB and Gravity Forms merge tags.
	$rules = array(
		array(
			'service_id'  => 42, // The GPB service this booking is for.
			'form_id'     => 1,
			'title'       => '{:3} - {:5}',
			'description' => 'Booking for {:3} - {gpb_datetime}',
		),
		array(
			'service_id'  => 42,
			'form_id'     => 2,
			'title'       => '{:7} with {gpb_service}',
			'description' => 'Location: {:9}',
		),
	);

	$entry = $booking->get_entry();
	if ( ! $entry ) {
		return $event_data;
	}

	$form_id    = (int) rgar( $entry, 'form_id' );
	$service_id = (int) $booking->get_service_id();

	foreach ( $rules as $rule ) {
		if ( (int) $rule['form_id'] !== $form_id || (int) $rule['service_id'] !== $service_id ) {
			continue;
		}

		if ( ! empty( $rule['title'] ) ) {
			$event_data['summary'] = \GP_Bookings\Merge_Tags::apply_template( $booking, $rule['title'] );
		}

		if ( ! empty( $rule['description'] ) ) {
			$event_data['description'] = \GP_Bookings\Merge_Tags::apply_template( $booking, $rule['description'] );
		}

		break;
	}

	return $event_data;
}, 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.