Created
July 25, 2024 05:03
-
-
Save shameemreza/c05b86c5460ecf97fb49d89d2b820d3c to your computer and use it in GitHub Desktop.
Automatically cancel orders after one hour in WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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