Home / Admin / WooCommerce Programmatically Cancel Pending Orders After 1 Hour
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce Programmatically Cancel Pending Orders After 1 Hour

Post More Detail
It’s possible that you’re thinking, “But I can already do that using the WooCommerce settings!” That’s true, you can set the “Hold Stock Minutes” value by going to WooCommerce Settings > Products > Inventory. After that time has elapsed, unpaid orders will be cancelled, and the stock will be restored to its original value.

However, what if you don’t want to use the “Hold Stock Minutes” feature? What if you don’t use stock management at all? In that case, orders will not be automatically cancelled, which could lead to problems with inventory management.

Moreover, suppose you need to perform conditional actions, such as cancelling only “failed” orders while leaving “pending” ones unchanged. In that case, the “Hold Stock Minutes” option won’t be of much help since you need to specify the order status you wish to target and then run the cancellation function accordingly.

This code is an implementation of the feature we discussed earlier – the ability to conditionally cancel pending orders periodically within a specified time frame, such as within one hour, in a WooCommerce store.

The first line of the code, add_action( ‘woocommerce_order_status_pending’, ‘woo_cancel_failed_pending_order_event’ );, registers an action hook that triggers when an order’s status changes to “pending.”

The second function, woo_cancel_failed_pending_order_event, is called whenever the “woocommerce_order_status_pending” action hook is triggered. This function checks whether a scheduled event already exists to cancel the order after one hour, and if not, it schedules a new event using wp_schedule_single_event(). This new event will trigger the woo_cancel_order function one hour after the order has been placed.

The woo_cancel_order function is the actual function that cancels the order. It is triggered by the “woo_cancel_failed_pending_order_after_one_hour” event hook. First, it retrieves the order object using wc_get_order(). Then, it clears the scheduled event to avoid any future cancellations. Finally, if the order’s status is “pending,” it updates the order status to “cancelled” using the update_status() method, with a note explaining why the order was cancelled.

In summary, this code adds a conditional cancellation feature for pending orders in a WooCommerce store, which automatically cancels orders that have been in a “pending” status for more than one hour.

Code Preview
php
<?php
add_action('woocommerce_order_status_pending', 'woo_schedule_cancel_order_event');
function woo_schedule_cancel_order_event($order_id) {
   if (!wp_next_scheduled('woo_cancel_pending_order', array($order_id))) {
      wp_schedule_single_event(time() + 3600, 'woo_cancel_pending_order', array($order_id));
   }
}
add_action('woo_cancel_pending_order', 'woo_cancel_order');
function woo_cancel_order($order_id) {
   $order = wc_get_order($order_id);
   wp_clear_scheduled_hook('woo_cancel_pending_order', array($order_id));
   if ($order && $order->has_status('pending')) {
      $order->update_status('cancelled', 'Pending order cancelled after 1 hour');
   }
}

Comments

Add a Comment