Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameemreza/c05b86c5460ecf97fb49d89d2b820d3c to your computer and use it in GitHub Desktop.
Save shameemreza/c05b86c5460ecf97fb49d89d2b820d3c to your computer and use it in GitHub Desktop.
Automatically cancel orders after one hour in WooCommerce
add_action( 'woocommerce_order_status_pending', 'wcwiz_cancel_failed_pending_order_event' );
function wcwiz_cancel_failed_pending_order_event( $order_id ) {
if ( ! wp_next_scheduled( 'wcwiz_cancel_failed_pending_order_after_one_hour', array( $order_id ) ) ) {
wp_schedule_single_event( time() + 3600, 'wcwiz_cancel_failed_pending_order_after_one_hour', array( $order_id ) );
}
}
add_action( 'wcwiz_cancel_failed_pending_order_after_one_hour', 'wcwiz_cancel_order' );
function wcwiz_cancel_order( $order_id ) {
$order = wc_get_order( $order_id );
wp_clear_scheduled_hook( 'wcwiz_cancel_failed_pending_order_after_one_hour', array( $order_id ) );
if ( $order->has_status( array( 'pending' ) ) ) {
$order->update_status( 'cancelled', 'Pending order cancelled after 1 hour' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment