在用户登录12小时后运行SQL(更新)。WooCommerce用户

时间:2021-02-24 作者:Nabil Dabbagh

如何在用户登录12小时后运行sql(更新)(对于Woocommerce用户)

用户登录12小时后,我想运行sql/函数来更新数据库中的值。

将值再次设置为“0”。

1 个回复
SO网友:Coder At Heart

您可以这样做(此代码尚未测试)。

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\') ;