WooCommerce: Automatically Cancel Orders

You may wondering – “but I can already do that from the WooCommerce settings!“. Yes, that’s correct; go to WooCommerce Settings > Products > Inventory and set the “Hold Stock Minutes” value. After that period, unpaid orders will be marked as cancelled to make sure the stock goes back to the initial value.

The problem is – what if you don’t want to use the “Hold Stock Minutes” thing, and even better, what if you don’t use stock management at all? In that case, orders won’t be marked as cancelled automatically.

Also, what if you need to do conditional work e.g. you only want to cancel “failed” orders, while you want to keep “pending” ones as they are? Even in this case, the “hold stock” option won’t work, as you need to specify which order status you want to target and then run the cancel function.

Either way, enjoy!

Let’s schedule an event that automatically cancels WooCommerce unpaid orders of a certain order status e.g. Failed Orders.

PHP Snippet: Programmatically Cancel Orders After 1 Hour

Please note, I’ve used the “woocommerce_order_status_{status}” hook, which means you can target any order status you wish (in my case, “woocommerce_order_status_pending” in order to automatically cancel Pending Payment orders after 1 hour).

If you need to edit the time period, simply change “3600” (one hour in seconds) to whatever you like.

/**
 * @snippet       Automatically Cancel Pending Orders After 1h
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_order_status_pending', 'bbloomer_cancel_failed_pending_order_event' );
 
function bbloomer_cancel_failed_pending_order_event( $order_id ) {
	if ( ! wp_next_scheduled( 'bbloomer_cancel_failed_pending_order_after_one_hour', array( $order_id ) ) ) {
		wp_schedule_single_event( time() + 3600, 'bbloomer_cancel_failed_pending_order_after_one_hour', array( $order_id ) );
	}
}

add_action( 'bbloomer_cancel_failed_pending_order_after_one_hour', 'bbloomer_cancel_order' );

function bbloomer_cancel_order( $order_id ) {
	$order = wc_get_order( $order_id );
	wp_clear_scheduled_hook( 'bbloomer_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' );
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Automatically Cancel Orders

  1. HI, This work also for failed orders?

    array( ‘failed’ ?

    1. Yes!

      array( 'pending', 'failed' )
  2. Hello Rodolfo! Great blog, you helped me alot already :). One quick question – can I change

    array( 'pending'

    to

    array( 'processing'

    to apply the same code to processing orders?

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *