How to run custom code when booking started?

Hi,
how can I schedule to run a custom code (function) for example 24 hours before every booking start?
Thank you so much in advance!
Emilio

Hi,

Unfortunately, there is no simple snippet here, but we can provide general recommendations. You can use this hook hivepress/v1/models/booking/update_status to create a function that will check the following: if the status of a new booking is publish, then schedule your function using the scheduler action by booking date.

I believe this will be useful to you.

Hi Andrii,
Thank you for the reply.

I did a test like this:


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



        error_log('test 4');
        if($new_status === 'trash'){

            error_log('test 2');
            hivepress()->scheduler->add_action( 'my_custom_functionnnnn', [ $booking_id ], time() + 120);
            error_log('test 3');
        }
	},
	1000,
	4
);

function my_custom_functionnnnn() {
    // Your custom code here
    
    error_log('test 0');
}

Then i cancelled a booking manually i get all the logs except “test 0” after the 2 minutes as i wanted.

Calling “hivepress()->scheduler->add_action( ‘my_custom_functionnnnn’, [ $booking_id ], time() + 120);” is correct? Should i replace maybe “my_custom_functionnnnn” with a hook?

Thank you so much.

Hi,

Please note that the add_action scheduler only registers a new hook, and the first parameter is not the name of the function but the type of hook you created. After that, you need to bind your function to this hook via add_action. Also, we recommend checking if your hook is registered through Scheduled Actions in WP Dashboard > Tools.

​I hope this is helpful to you.

1 Like

Hi andrii,
Thank you so much, you helped me a lot!

If someone needs here is a working example:

// Define the action hook
function my_custom_hook() {
    do_action('my_custom_hook');
}

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

        if($new_status === 'trash'){

            hivepress()->scheduler->add_action( 'my_custom_hook', [ $booking_id ], time() + 120 );
        }
	},
	1000,
	4
);

// Hook your function to the custom action
add_action('my_custom_hook', 'my_custom_function_o');

// Your custom function
function my_custom_function_o() {
    // Your custom code here
    error_log('test 0');
}

Thanks again, have a nice day!
Emilio

1 Like

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