WooCommerce Programmatically Cancel Pending Orders After 1 Hour

Connect With Us In Our Social Media

Table of Contents

Post Intro

If you are running an e-commerce store using the popular WooCommerce platform, you may want to learn how to conditionally cancel pending orders periodically within a specified time frame, such as within 1 hour. This can help you manage your inventory and ensure that you only fulfill orders that are likely to be paid for. By setting up an automatic process to cancel orders that remain in a pending status for too long, you can avoid wasting time and resources on orders that are unlikely to be completed. In this article, we will walk you through the steps you need to take to set up this feature for your WooCommerce store.

Youtube Tutorial

youtube tutorial will be available soon.

Login To Access Code Snippet:

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.

Conclusion In summary

Features added:
The feature allows for conditional cancellation of pending orders within a specified time frame in a WooCommerce store.

It helps manage inventory and ensures that only orders that are likely to be paid for are fulfilled in a WooCommerce store.

An automated process cancels orders that remain in a pending status for too long in a WooCommerce store.

The feature provides more flexibility in managing pending orders by allowing for conditional cancellation of specific order statuses in a WooCommerce store.

It is especially useful for e-commerce stores that don’t use stock management or don’t want to use the “Hold Stock Minutes” feature in WooCommerce.

This feature can be set up easily on a WooCommerce store to save time and resources.

Stay UpTo Date with Latest Post And news:

0
Would love your thoughts, please comment.x
()
x

Login To Access Code Snippet:

Stay UpTo Date with Latest Post And news: