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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @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

  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: How to Add a Custom Order Status
    All WooCommerce orders go to either “processing“, “completed“, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]
  • WooCommerce: Add Column to Orders Table @ WP Dashboard
    The WooCommerce Orders Table, which can be found under WP Dashboard > WooCommerce > Orders, provides us with 7 default columns: Order – Date – Status – Billing – Ship to – Total – Actions. This is used by shop managers to have an overview of all orders, before eventually clicking on a specific one. […]
  • WooCommerce: Hide/Show The WP Admin Bar
    In previous WooCommerce versions, new customers could access the WP Admin black bar after purchase. Now this seems fixed. Still, what about other user roles, and what if you want to override this default behavior? Well, here’s a quick snippet for you – feel free to use it in your own WooCommerce site. Enjoy!
  • WooCommerce: Display Custom Filters @ WP Dashboard > Products
    If you go to WordPress Dashboard > Products you will find default product admin filters such as “Select a category”, “Filter by product type”, “Filter by stock status”. What if you want to add more custom filters to let your shop managers find products easily? For example, you could add “Filter by product tag” (“product […]

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

2 thoughts on “WooCommerce: Automatically Cancel Orders

  1. 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 *