2.Hooks

2.1Introduction

TeamBooking has some useful WordPress hooks that you can use in order to deeply customize the aspect, add custom content or trigger custom actions at certain points.

Note: that section requires some very basic programming skills. The WordPress actions/filters usage is well documented, anyway in order to take the full advantage of the TeamBooking objects and perform operations on them, you need to know the structure of them. Feel free to ask questions to our support.

Warning: any custom come must be placed in a custom plugin, or in your child theme's functions.php file. Placing custom code in one of the TeamBooking's files, will result in that code being deleted after an update.

2.2Action hooks

Those are the hooks used to perform custom actions at certain point of the reservation flow.

2.2.1tbk_reservation_before_processing

Fired right before processing a reservation. You can use this hook, for example, if you want to implement your custom code to allow or prevent the reservation depending on some conditionals.

Examples
  • How to prevent a customer to perform a reservation if his first name is "John".

    Want to test the code and play with it? Download this example as a WordPress plugin

    Let's add the action in our code (in your functions.php file, a plugin, everywhere):

    add_action('tbk_reservation_before_processing', 'example_one_func');
                    

    Each time a customer presses the "Book now" button (before the reservation's review appears), the example_one_func function, which must be defined, is called:

    function example_one_func($reservation_data){
        $form_fields = $reservation_data->getFieldsArray();
        if (isset($form_fields['first_name']) && $form_fields['first_name'] === 'John') {
            $reservation_data->stop = TRUE;
            echo "I'm sorry John...";
        }
    }
                    
  • How to send an e-mail to the admin as soon as a customer presses the "Book now" button.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_reservation_before_processing', 'example_two_func');
                    
    function example_two_func($reservation_data){
        // The $reservation_data is not used in this example, but you can be creative
        $subject = 'A customer is going to make a reservation!';
        $message = "Will he/she confirm or not? Who knows...";
        // Send email to admin.
        wp_mail( 'admin@example.com', $subject, $message );
    }
                    
  • How to skip the "Review reservation's data" step (be careful doing that!)

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_reservation_before_processing', 'example_three_func');
                    
    function example_three_func($reservation_data){
        // that's quite simple...
        $reservation_data->skip_review = TRUE;
    }
                    

2.2.2tbk_calendar_click_on_day

Fired right after clicking on a day on the calendar.

Examples
  • How to send the customer directly to the reservation form if the day contains one slot only.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_calendar_click_on_day', 'example_func');
                    
    function example_func($params){
        $slots = $params->getSlots();
        if (count($slots) === 1) {
            $params->override_schedule = TRUE; //!\\important
            $form = TeamBooking\Frontend\Form::fromSlot($slots[0]);
            echo $form->getContent();
        }
    }
                    

2.2.3tbk_schedule_slot_parse

Used to perform operation to the slot before passing it to the frontend calendar.

Note: changes to the slots will affect also requests made by the REST API

Examples
  • How to change the displayed address of the slots for a certain service.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_schedule_slot_parse', 'example_one_func');
                    
    function example_one_func($slot){
        if ($slot->getServiceId() === 'music-class') {
            $slot->setLocation('5th Ave, New York');
        }
    }
                    
  • How to hide a slot if it comes from a certain Coworker

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_schedule_slot_parse', 'example_two_func');
                    
    function example_two_func($slot){
        if ($slot->getCoworkerId() === 2) {
            $slot->show = FALSE;
        }
    }
                    

2.2.4tbk_reservation_email_to_admin

Called just before the notification e-mail to the Admin is prepared. Functions hooked to it can manipulate two arguments: the email object and the reservation's data object.

Examples
  • Add a BCC e-mail address if the reservation is for a particular service.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_reservation_email_to_admin', 'example_func', 10, 2);
                    
    function example_func($email, $reservation){
        // if the reservation is for "gym-class", let's add our BCC
        if ($reservation->getServiceId() === 'gym-class') {
            $email->setBcc('john@example.com', 'John Gym');
        }
    }
                    

2.2.5tbk_reservation_email_to_coworker

Called just before the notification e-mail to the Coworker is prepared. Functions hooked to it can manipulate two arguments: the email object and the reservation's data object.

Examples
  • Don't send the e-mail to the Coworker

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_reservation_email_to_coworker', 'example_func', 10, 2);
                    
    function example_func($email, $reservation){
        $email->stop = TRUE;
    }
                    

2.2.6tbk_reservation_email_to_customer

Called just before the confirmation e-mail to the Customer is prepared. Functions hooked to it can manipulate two arguments: the email object and the reservation's data object.

Examples
  • Add a CC e-mail address if the reservation is for a particular service.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_reservation_email_to_customer', 'example_func', 10, 2);
                    
    function example_func($email, $reservation){
        // if the reservation is for "music-class", let's add our CC
        if ($reservation->getServiceId() === 'music-class') {
            $email->setCc('john@example.com', 'John Music');
        }
    }
                    

2.2.7tbk_cancellation_email_to_customer

Called just before the cancellation e-mail to the Customer is prepared. Functions hooked to it can manipulate two arguments: the email object and the reservation's data object.

Example not provided given the similarity to the previous actions (e.g. tbk_reservation_email_to_customer).

2.2.8tbk_cancellation_email_to_admin

Called just before the cancellation e-mail to the Admin (and to the Service Provider) is prepared. Functions hooked to it can manipulate two arguments: the email object and the reservation's data object.

Example not provided given the similarity to the previous actions (e.g. tbk_reservation_email_to_customer).

2.3Content hooks

Those are the hooks used to add custom content at certain parts of the frontend.

2.3.1tbk_schedule_slot_render

Used to append custom content to the slots in the slots list.

Examples
  • How to show an advice at the bottom of the slot if it falls within a certain timeframe.

    Want to test the code and play with it? Download this example as a WordPress plugin

    add_action('tbk_schedule_slot_render', 'example_func');
                    
    function example_func($slot){
        // defining our time bounds
        $start = DateTime::createFromFormat('H:i', '08:30');
        $end = DateTime::createFromFormat('H:i', '09:30');
        // fetch the slot's start time and set the timezone
        $slot_start = new DateTime($slot->getStartTime());
        $slot_start->setTimezone(Toolkit\getTimezone());
        // our condition
        if (strtotime($slot_start->format('H:i')) >= strtotime($start->format('H:i'))
            && strtotime($slot_start->format('H:i')) < strtotime($end->format('H:i'))
        ) echo "Beware of booking this one, it's very early in the morning!";
    }
                    

2.3.2tbk_email_hook_replace

It is called every time a hook (text between square brackets) is found in the e-mail body. You can intercept all the content between square brackets and do some pretty handy customizations. So you can also create your own hooks and replace them with your own logic!

Examples
  • How to add a "genitive" case to the Customer's first name.

    Want to test the code and play with it? Download this example as a WordPress plugin

    So, let's assume you need a custom hook in your e-mail body template like [first_name_genitive] that adds ' or 's at the end depending on the name itself.

    In English, you can just write something like that: [first_name]'s in your e-mail body. That's why we are considering the German language for our example.

    You already have the [first_name] hook, but it only prints the first name as is. And if you write an hook like [first_name_genitive] TeamBooking will ignore it, then what?

    Simple. Just code your own logic to intercept your custom hook, and do wathever string operation you need. Let's see how.

    add_action('tbk_email_hook_replace', 'my_custom_hook_filter', 10, 3);
                    
    function my_custom_hook_filter($value, $hook, $all_values)
    {
        /* The $hook variable is the current hook intercepted by TeamBooking,
         * basically every text between square brackets is a hook. So we need
         * to check when this hook is equal to our "first_name_genitive" (we must
         * have placed it in the e-mail template, of course, otherwise it won't be here).
         *
         * The $value is the text that TeamBooking will render at the hook's place.
         * If the hook is not recognized by TeamBooking (i.e. our custom one)
         * then $value will be the hook text itself (i.e. it won't be modified).
         *
         * The $all_values is an array ("hook" => "value") of all the values
         * known by TeamBooking, that are ready to be rendered.
         *
         * Our function must ALWAYS return the text that should be rendered
         * at the current hook's place.
         */
    
        if (trim($hook, '[]') === 'first_name_genitive') {
    
            // That's our hook... let's retrieve the customer's first name
            $first_name = $all_values['first_name'];
    
            // Let's code out German genitive case logic
            if (preg_match('/(s|ß|z|x|ce)$/', $first_name)) {
    
                // Return the wanted replacement text of current "first_name_genitive" hook
                return $first_name . "'";
    
            } else {
                // Return the wanted replacement text of current "first_name_genitive" hook
                return $first_name . "'s";
    
            }
        } else {
    
            // That's not our hook, so just return its default $value. If we forget
            // to return it, then all the other hooks will be replaced by an empty string!!
            return $value;
        }
    }