Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. /**
  2.  * Cancela o pedido pendende depois de algum tempo
  3.  */
  4. function cancel_pending_order_after_x_days() {
  5.     $limit = 4;
  6.  
  7.     global $wpdb;
  8.  
  9.     $expired_payment_orders = $wpdb->get_col($wpdb->prepare("
  10.             SELECT posts.ID
  11.             FROM {$wpdb->posts} AS posts
  12.             WHERE posts.post_status = 'wc-pending'
  13.             AND posts.post_date < %s
  14.     ", date('Y-m-d H:i:s', strtotime('-' . $limit . ' days'))));
  15.  
  16.     if ($expired_payment_orders) {
  17.         foreach ($expired_payment_orders as $order) {
  18.             $order = wc_get_order($order);
  19.             $order->update_status('cancelled', 'Pedido foi expirado.');
  20.         }
  21.     }
  22. }
  23.  
  24. // Agenda a verificação
  25. function init_cancel_pending_order_after_x_days() {
  26.     if (!wp_next_scheduled('schedule_cancel_pending_order_after_x_days')) {
  27.         wp_schedule_event(time(), 'daily', 'schedule_cancel_pending_order_after_x_days');
  28.     }  
  29. }
  30. add_action('init', 'init_cancel_pending_order_after_x_days');
  31.  
  32. // Excecuta a verificação
  33. add_action('schedule_cancel_pending_order_after_x_days', 'cancel_pending_order_after_x_days');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement