Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function get_unpaid_orders() {
  2. global $wpdb;
  3.  
  4. $unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
  5. SELECT posts.ID
  6. FROM {$wpdb->posts} AS posts
  7. WHERE posts.post_status = 'wc-on-hold'
  8. AND posts.post_date < %s
  9. ", date( 'Y-m-d H:i:s', strtotime('-1 minute') ) ) );
  10.  
  11. return $unpaid_orders;
  12. }
  13.  
  14. add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
  15. function cancel_unpaid_orders() {
  16. $unpaid_orders = get_unpaid_orders();
  17.  
  18. if ( $unpaid_orders ) {
  19. foreach ( $unpaid_orders as $unpaid_order ) {
  20. $order = wc_get_order( $unpaid_order );
  21. $cancel_order = true;
  22.  
  23. foreach ( $order->get_items() as $item_key => $item_values) {
  24. $manage_stock = get_post_meta( $item_values, '_manage_stock', true );
  25. if ( $manage_stock == "yes" ) {
  26. $payment_method = $order->get_payment_method();
  27. if ( $payment_method == "bacs" ) {
  28. $cancel_order = false;
  29. }
  30. }
  31. }
  32. if ( $cancel_order == true ) {
  33. $order -> update_status( 'cancelled', __( 'The order was cancelled due to no payment from customer.', 'woocommerce') );
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement