wc_cancel_unpaid_orders

Cancel all unpaid orders after held duration to prevent stock lock for those products.

Description

wc_cancel_unpaid_orders(); 

Usage

  1. if ( !function_exists( 'wc_cancel_unpaid_orders' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-order-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_cancel_unpaid_orders(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-order-functions.php  
  1. function wc_cancel_unpaid_orders() { 
  2. $held_duration = get_option( 'woocommerce_hold_stock_minutes' ); 
  3.  
  4. if ( $held_duration < 1 || 'yes' !== get_option( 'woocommerce_manage_stock' ) ) { 
  5. return; 
  6.  
  7. $data_store = WC_Data_Store::load( 'order' ); 
  8. $unpaid_orders = $data_store->get_unpaid_orders( strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) ); 
  9.  
  10. if ( $unpaid_orders ) { 
  11. foreach ( $unpaid_orders as $unpaid_order ) { 
  12. $order = wc_get_order( $unpaid_order ); 
  13.  
  14. if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === $order->get_created_via(), $order ) ) { 
  15. $order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', woocommerce ) ); 
  16. wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' ); 
  17. wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );