English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Fields  >  Auto Populate

apply_filters_ref_array( 'vikappointments_auto_populate_custom_fields', bool $status, array &$data, array $fields, mixed $user )

Fires while binding the values of the custom fields.


Description

Trigger hook to allow external plugins to auto-populate the custom fields with other details that are not supported by default by the user instance.

This filter does not fire in case some details have been already collected by the system. In a few words, this filter is fired only during the first booking of a new customer.


Parameters

$status

(bool)  True on success, false otherwise.

&$data

(array)  Where to inject the fetched data. The keys of the array are equals to the name of the custom fields.

$fields

(array)  An array of custom fields.

$user

(mixed)  The user instance.


Example

This example auto-fill the address custom field with the street name assigned to the currently logged-in user, assuming that $user->address has been supported by a third party plugin.

/**
 * Trigger hook to allow external plugins to auto-populate the custom fields
 * with other details that are not supported by default by the user instance.
 *
 * @param  boolean  $status  True on success, false otherwise.
 * @param  mixed    &$data   Where to inject the fetched data.
 * @param  array    $fields  The custom fields list to display.
 * @param  mixed    $user    The details of the user.
 */
add_filter('vikappointments_auto_populate_custom_fields', function($status, &$data, $fields, $user)
{
    if (empty($user->address))
    {
        // make sure the address is not empty
        return false;
    }

    $addrFields = array();

    // search for an address field
    foreach ($fields as $cf)
    {
        if ($cf['rule'] == 'address')
        {
            $addrFields[] = $cf;
        }
    }

    if ($addrFields)
    {
        // fetch field key
        $key = $addrFields[0]['name'];

        // register address value with the address assigned to the user
        $data[$key] = $user->address;
    }

    return $status;
}, 10, 4);

Changelog

Version Description
1.2 Introduced.
Last Update: 2024-07-12
Helpful?