title: How to cancel a pending payment order after x Minutes or x hour in WooCommerce
body: <p>Continuation of <a href="https://stackoverflow.com/questions/55792360/automatically-cancel-order-after-x-days-if-no-payment-in-woocommerce">this Question</a> Answered by @LoicTheAztec was a great solution. But I need to do some update on it. I want to update it the time delay to 30min or 1 hr instead of 5 days.</p>
  
  <p>Also it should work on order status pending/on-hold. </p>
  
  <p>The problem is when I try to use this code for "pending payment" every day when it execute the very last order auto canceled. </p>
  
  <p>So the code should avoid the very last order or should check the order place time.  </p>
  
  <pre><code>add_action( 'woocommerce_order_status_changed', 'daily_cancel_unpaid_orders', 10, 4 );
      function daily_cancel_unpaid_orders( $order_id, $old_status, $new_status, $order ) {
          // Enable the process to be executed daily
          if( in_array( $new_status, array('processing', 'completed') ) 
              &amp;&amp; get_option( 'unpaid_orders_daily_process' ) &lt; time() ) :
  
          $days_delay = 1; // &lt;=== SET the delay (number of days to wait before cancelation)
  
          $one_day    = 24 * 60 * 60;
          $today      = strtotime( date('Y-m-d') );
  
          // Get unpaid orders (5 days old)
          $unpaid_orders = (array) wc_get_orders( array(
              'limit'        =&gt; -1,
              'status'       =&gt; 'pending',
              'date_created' =&gt; '&lt;' . ( $today - ($days_delay * $one_day) ),
          ) );
  
          if ( sizeof($unpaid_orders) &gt; 0 ) {
              $cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
  
              // Loop through WC_Order Objects
              foreach ( $unpaid_orders as $unpaid_order ) {
                  $order-&gt;update_status( 'cancelled', $cancelled_text );
              }
          }
          // Schedule the process to the next day (executed once restriction)
          update_option( 'unpaid_orders_daily_process', $today + $one_day );
  
          endif;
      }
  </code></pre>