wc_cancel_unpaid_orders()
Cancel all unpaid orders after held duration to prevent stock lock for those products.
Hooks from the function
Return
null
. Ничего (null).
Usage
wc_cancel_unpaid_orders();
wc_cancel_unpaid_orders() wc cancel unpaid orders code WC 8.3.0
function wc_cancel_unpaid_orders() { $held_duration = get_option( 'woocommerce_hold_stock_minutes' ); // Re-schedule the event before cancelling orders // this way in case of a DB timeout or (plugin) crash the event is always scheduled for retry. wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' ); $cancel_unpaid_interval = apply_filters( 'woocommerce_cancel_unpaid_orders_interval_minutes', absint( $held_duration ) ); wp_schedule_single_event( time() + ( absint( $cancel_unpaid_interval ) * 60 ), 'woocommerce_cancel_unpaid_orders' ); if ( $held_duration < 1 || 'yes' !== get_option( 'woocommerce_manage_stock' ) ) { return; } $data_store = WC_Data_Store::load( 'order' ); $unpaid_orders = $data_store->get_unpaid_orders( strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) ); if ( $unpaid_orders ) { foreach ( $unpaid_orders as $unpaid_order ) { $order = wc_get_order( $unpaid_order ); if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === $order->get_created_via(), $order ) ) { $order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) ); } } } }