0

How can I Run a sql (update) after 12 hours after the user login (for Woocommerce users)

After 12 hours after a user login, I want to run a sql/ function to update a value in the database.

To set a value to '0' again.

1 Answer 1

0

You could do something like this (this code has not been testedt).

function schedule_sql_command($user_login, $user) { 
  // 12 hours in seconds.
  $from_now = 12 * 60 * 60;
  // schedule a call to a custom hook passing in the user's ID
  wp_schedule_single_event(time() + $from_now, 'run_sql_command', array( $user->ID) );
}

// this is the custom hook that takes the user_id as a param
function run_sql_command($user_id) { 
 global $wpdb;
 $wpdb->query("Your query here using $user_id"); 
}
// Hook when user logs in
add_action( 'wp_login', 'schedule_sql_command') ; 
3
  • It doesn't work even though it seems a good solution. The code breaks up Feb 25, 2021 at 13:53
  • I think do_action() is the problem. Should write add_action(). but it doesn't change the vlaue in the databse anway :( Feb 26, 2021 at 8:07
  • Yeah it should have been add_action() code updated. Try using the crontrol plugin to see scheduled events Feb 26, 2021 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.