How to add cancel order button to WooCommerce customer dashboard

This tutorial will guide you through allowing your WooCommerce customers to cancel their order by adding a cancel order button in the WooCommerce customer dashboard.

You may have clients who use WooCommerce and they need their customers to be able to cancel the orders. This can happen if customer wants to change his order for whatever reason. If you are running a membership site, you might want to let your customers change their membership level as well.

If a customer places an order and then wants to cancel it before it’s shipped, the best way is to add a cancel button in their account page. In this article we will show you how to create a WooCommerce cancel order button.

woocommerce allow customer to cancel order

WooCommerce by default includes an option to cancel orders, but it is only available for orders with order status pending or failed. Also there are WordPress plugins that provide this kind of functionality out of the box. But in today’s tutorial we are going to see how you can add your own custom cancel order button without the need of additional plugins. All we need is a few lines of code. You can add the code snippet to functions.php file of your child WordPress theme or via a dedicated plugin.

Fortunately for us, there is a very easy way to modify the default statuses for which order cancellation is available, just by using the

woocommerce_valid_order_statuses_for_cancel filter. This filter is called for each order and passes two arguments:

  • $statuses – an array containing the default cancellable statuses, defaults to: array('pending','failed')
  • $order – the order object

Let’s see a simple example where we allow any order with status pending, processing or on hold to be cancelled by the customers:

add_filter('woocommerce_valid_order_statuses_for_cancel', 'my_cancellable_statuses', 10, 2);

function my_cancellable_statuses($statuses, $order){
  return array('pending', 'processing', 'on-hold');
}

And the result is a Cancel button next to any order that is with from the specified statuses:

WooCommerce allow customer to cancel order

This filter can be further customised. What we can do is apply our custom cancellable statuses only when a certain condition is met. For example, we can apply our custom statuses only to recent orders. Here is an example where we apply our custom cancellable statuses only for orders that were made in the last 12 hours:

add_filter('woocommerce_valid_order_statuses_for_cancel', 'my_cancellable_statuses', 10, 2);

function my_cancellable_statuses($statuses, $order){
  $freshness_interval = 12*HOUR_IN_SECONDS;
  $order_date = strtotime($order->get_date_modified());
  $is_order_recent = ( $order_date + $freshness_interval ) >= strtotime('now');

  if($is_order_recent){
    // return our custom statuses
    return array('pending', 'processing', 'on-hold');
  }else{
    // return the default statuses
    return $statuses;
  }
}

And to set the interval that identifies whether an order is recent in days you can use this code instead:

$freshness_interval = 5*DAY_IN_SECONDS;

How to find all customers who have cancelled order

If you want to find all customers who have had order cancellation in your WooCommerce store, you can use the Users Insights filters. For example:

woocommerce customers cancel order

If you want to find all the customers who have had order cancellation request of a specific product you can use the “Placed order” filter and than select the specific product and order status to be canceled:

woocommerce customers cancel order

Conclusion

Today we dived into how you can add cancel order button into your WooCommerce customer dashboard. This allows your users to have more flexibility in their order cancellation request. In addition, we explored how you can use Users Insights to find users who have cancelled orders or have orders with specific  custom order status.

We hope you enjoyed, and see you again next time!