2

I'm trying to make an SQL query to run upon cancellation of a booking, via a custom plugin, updating custom user meta data.

Here's my code:

function wporg_callback() {
    global $wpdb;

    $wpdb->query("UPDATE usermeta SET meta_value = 15 WHERE umeta_id = 131");
}

add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');

But It's not working.
Is there something wrong with the query? Is the right action not being used?


Edit - I Also tried the following without success:

add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback'); 
function wporg_callback( $booking_id ) { 
    // Add/Update custom user meta data 
    update_user_meta( 2, 'download_credits', 17 ); 
} 
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
solo22x
  • 23
  • 5
  • Ok, in the usermeta table, I'm updating the meta_value to 15, for the row where umeta_id is 131 – solo22x Jun 15 '20 at 16:18
  • Hi, so I'm trying to get this to work from a custom plugin I've built. I've taken your advice and updated my code to the following:add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback'); function wporg_callback( $booking_id ) { // Add/Update custom user meta data update_user_meta( 2, 'download_credits', 17 ); } – solo22x Jun 15 '20 at 18:10
  • I am only trying to check if the woocommerce_bookings_cancelled_booking hook is registering for me, and so far it is not working. I've double checked to make sure the arguments for the update_user_meta are correct – solo22x Jun 15 '20 at 18:12
  • I was replying to someone else's comments, which they deleted. – solo22x Jun 15 '20 at 22:06
  • Ok, in any case you should probably clarify your question further: what you have done to debug, what "it's not working" means etc. – shox Jun 15 '20 at 22:12
  • I finaly found the correct hook to be used… This time it works fine: I get the correct user Id from the booking and the user custom meta data is saved to database. – LoicTheAztec Jun 15 '20 at 22:27

1 Answers1

1

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:

  • An SQL query is not needed to add/update custom user meta data, as you can use instead the dedicated Wordpress function update_user_meta().

  • I couldn't make it work using woocommerce_bookings_cancelled_booking hook.


WooCommerce Bookings action and filters hooks developer documentation

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thankyou, I'm actually using it in my plugin but it still seems to work. Would you happen to know how I can get the booking data, specifically the cost, via the booking id? – solo22x Jun 15 '20 at 22:38