php - What hook to be used for cancelled booking in WooCommerce Bookings?

one text

Solution:

Updated:

The correct hook to be used is woocommerce_booking_cancelled (a composite hook) that will allow you to retrieve the user ID and to Add/update custom user meta data like below:

add_action('woocommerce_booking_cancelled', 'booking_cancelled_transition_to_callback', 10, 2 );
function booking_cancelled_transition_to_callback( $booking_id, $booking ) {
    // Get the user ID from the Booking ID
    $user_id = get_post_field ('post_author', $booking_id);
    
    $download_credits = get_post_meta( $booking_id, '_booking_cost', true );
    
    // Add/Update custom user meta data
    update_user_meta( $user_id, 'download_credits', $download_credits );
}

Code goes in functions.php file of your active child theme (or theme). Tested and works.


How to get the booking data (from the booking ID displayed in the order):

  • Go to your database under wp_postmeta table to get the desired meta_key from the post_id (that is the booking ID)

  • Use that meta key in get_post_meta() WordPress function like:

     $meta_value = get_post_meta($booking_id, 'the_meta_key', true); 
    

Notes:


WooCommerce Bookings action and filters hooks developer documentation

Source