How Can Customer Cancel Order WooCommerce

How Can Customer Cancel Order WooCommerceDo you want to allow customers to cancel orders in your WooCommerce store? Then stick to the end, as this post aims to provide you with a solution we made specifically to solve this problem. We will not be using a plugin. Therefore, you need some coding experience to implement this solution. However, we will explain each step in detail to help you out.

If you sell physical products or services online, there is a very high chance that customers will mess up their orders. This means that the store owner needs to cancel these orders.

Fortunately, WooCommerce allows you to cancel an order easily. However, the customer needs to contact you for cancellation. This means that they have to provide you with their details, and then you can navigate to orders and locate the order you wish to delete.

If this happens regularly, it can be very tiring, especially if you have many orders. This is why you need to allow your customers to cancel their orders to make it easier for you. However, WooCommerce does not have this functionality. This is why we created this post to help you out.

How Can Customer Cancel Order WooCommerce

By the end of this post, you will be able to allow your customers to cancel their orders. To achieve this, we need to modify WooCommerce’s core files.

Therefore, you need to install or create a child theme on your WordPress installation. This will ensure that your changes are not lost during an update.

Let us get right into it.

Steps to Allow Customers to Cancel Orders in WooCommerce

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to allow customers to cancel their orders in the front end.
  3. Add the following code to the php file:
[php] add_filter( ‘woocommerce_valid_order_statuses_for_cancel’, ‘njengah_valid_order_statuses_for_cancel’, 10, 2 );
function njengah_valid_order_statuses_for_cancel( $statuses, $order ){
// Set HERE the order statuses where you want the cancel button to appear
$custom_statuses = array( ‘completed’, ‘pending’, ‘processing’, ‘on-hold’, ‘failed’ );
// Set HERE the delay (in days)
$duration = 3; // 3 days
// UPDATE: Get the order ID and the WC_Order object
if( isset($_GET[‘order_id’]))
$order = wc_get_order( absint( $_GET[‘order_id’] ) );
$delay = $duration*24*60*60; // (duration in seconds)
$date_created_time = strtotime($order->get_date_created()); // Creation date time stamp
$date_modified_time = strtotime($order->get_date_modified()); // Modified date time stamp
$now = strtotime("now"); // Now time stamp
// Using Creation date time stamp
if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
else return $statuses;
}
[/php]
  1. This is the outcome:cancel order

Wrapping Up

In today’s brief tutorial, we have discussed how you can allow your customers to cancel their orders. This will save you a lot of time and effort when customers make a mistake in their order.

However, you should be very careful when editing the functions file. If you make any mistake, a critical error will be displayed on your site.

If you need a custom solution for your site, feel free to contact us. We hope that this post helped you learn more about WooCommerce orders.

Similar Articles

  1. How to Upload Image On Product Page In WooCommerce