How to allow customers to cancel their orders in WooCommerce

woo expert

Table of Contents

The problem

By default, WooCommerce allows customers to cancel their orders themselves only if they are pending, but does not allow it if the order is in any other status: processing, completed, on hold, etc.

default order cancelling

Now, a lot of time is wasted if the customer wants to cancel an order in any other state, by not allowing him to do it himself. He would have to contact the store manager – if any contact method is visible – and then have to search for the order, cancel it manually, etc.

If your store has many sales and you want to allow customers to cancel their orders themselves in another order status other than “Pending” you have to add this functionality, not included in WooCommerce as I said.

The solution

To include this functionality we have to use a code that allows the cancellation of orders for other order statuses, apart from “pending”, and that also allows us to define some limits, such as a time limit to be able to cancel an order.

Here is an example code:

/* Allow customer to cancel orders */
add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'wphelp_cancel_order', 10, 2 );
function wphelp_cancel_order( $statuses, $order ){
$custom_statuses = array( 'completed', 'pending', 'processing', 'on-hold', 'failed' ); //Order status allowed to be cancelled
$duration = 30; //Maximum days from order to cancel allowed
if( isset($_GET['order_id']))
$order = wc_get_order( absint( $_GET['order_id'] ) );
$delay = $duration*24*60*60;
$date_created_time = strtotime($order->get_date_created());
$date_modified_time = strtotime($order->get_date_modified());
$now = strtotime("now");
if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
else return $statuses;
}Code language: PHP (php)

In the above code there are 2 lines that you will probably want to modify to suit your needs:

  • $duration = 30; – This is the age in days of the order so that the customer will see the cancel button and can easily cancel the order.
  • $custom_statuses = array( ‘value’ , ‘value’); – Here you must put a value for each status of the order in which the button will be shown, and the possibility to cancel the order.

Depending on the limits you define in these fields the order cancel buttons will be displayed or not on the customer account page.

If you need any help on where to put these codes.

custom order cancelling

How useful was this post?

Click on a smiley to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

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

Scroll to Top