Appointments + Prepopulate and redirects.

Couple of things I'm needing and some similar things have show up in the search but nothing exactly what I need.

1) I need to be able to pre-populate both the standard fields and the "additional fields" by grabbing from the URL. I know a little bit about the GET method but not at all in this case.
Example URL = http://example.com/?email=email@email.com&custom1=customval1&custom2=customval2

2) I need to have the same data and the appointment data pass to another page in submit. Instead of a popup like I get now I need it to take me to another page that loads all the existing variables as well as the appointment info as well.

  • Vladislav

    Hello,

    You can redirect your users after making an appointment to a different page by a bit of code like this:

    function app_redirect ($script) {
    	$page_id = 1312;
    	$url = !empty($page_id)
    		? "'" . get_permalink($page_id) . "'"
    		: 'app_location()'
    	;
    	return str_replace("window.location.href=app_location()", "window.location.href={$url}", $script);
    }
    add_filter('app_footer_scripts', 'app_redirect');

    This code assumes you have a page/post with ID 1312, so you'll want to tweak this part to better reflect your site's context. As for pre-filling the additional fields data, that's a bit harder but this code can hopefully help:

    function my_app_save_additional_fields ($app_id) {
    	global $appointments;
    	$fields = !empty($appointments->options['additional_fields']) ? $appointments->options['additional_fields'] : array();
    	if (empty($fields)) return false;
    	$cookie = array();
    	foreach ($fields as $field) {
    		if (empty($field['required'])) continue;
    		$name = preg_replace('/[^-_a-z0-9]/', '', strtolower($field['label']));
    		$cookie[$name] = $_POST[$name];
    	}
    	if (empty($cookie)) return false;
    	$expire = $appointments->local_time + 3600 * 24 * ( $appointments->options["app_limit"] + 365 );
    	$cookiepath = defined('COOKIEPATH') ? COOKIEPATH : '/';
    	$cookiedomain = defined('COOKIEDOMAIN') ? COOKIEDOMAIN : '';
    	@setcookie("wpmudev_appointments_additional_fields", serialize($cookie), $expire, $cookiepath, $cookiedomain);
    }
    add_action('app_new_appointment', 'my_app_save_additional_fields');
    
    function my_app_populate_additional_fields () {
    	if (empty($_COOKIE['wpmudev_appointments_additional_fields'])) return false;
    	$data = unserialize(stripslashes($_COOKIE['wpmudev_appointments_additional_fields']));
    	if (empty($data)) return false;
    	?>
    <script type="text/javascript">
    (function ($) {
    var field_data = <?php echo json_encode($data); ?>;
    function populate_fields () {
    	setTimeout(function () {
    		var $fields = $(".appointments-field-entry");
    		$.each(field_data, function (idx, el) {
    			var $field = $fields.filter('[data-name="' + idx + '"]');
    			if ($field.length) $field.val(el);
    		})
    	}, 300);
    }
    
    $(document).ajaxSend(function(e, xhr, opts) {
    	if (!opts.data) return true;
    	if (!opts.data.match(/action=pre_confirmation/)) return true;
    	xhr.success(populate_fields);
    });
    })(jQuery);
    </script>
    	<?php
    }
    add_action('app-footer_scripts-after', 'my_app_populate_additional_fields', 99);

    As you see, this is actually a two-step process, we first save the sent fields when an appointment is first made, then re-populate them on subsequent forms appearance with some injected javascript. I hope this helps.

  • wurv

    I'm still confused how this is grabbing data from the url. Where am I specifying that populates what? As in if my URL is: http://example.com/?email=email@email.com&custom1=customval1&custom2=customval2 where do I say customval1 email etc?

    I don't think this really needed cookies to make it happen, just some javascript on the calendar page to populate all the appointment fields from the URL and a little snipet to redirect and carry those parameters to a specified page. Also in the above code I assume that part goes in the page that we are working on, but does some go in functions.php?

  • Vladislav

    Hello,

    Actually, all the code above would go into your functions.php (or better yet, a mu-plugin). The code samples above are, of course, just a guideline to get you started in the right direction - we don't know the specifics of your scenario, neither exactly what needs to be populated, nor from which exact GET query parts. With the generic example you just mentioned, the GET query parsing would go into the "my_app_populate_additional_fields" function and you can use the $_GET array as $data, instead of the values populated from the cookie. In this scenario, you wouldn't also need the "my_app_save_additional_fields " function, as that's where the variables get saved in the cookie. Hopefully the code can get you started in the right direction.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.