Index 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 state: processing, completed, waiting, etc.
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. You 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 the possibility of canceling their orders themselves in other states of the order 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. order .
Here’s a sample code:
/ * Allow customer to cancel order * / add_filter ('woocommerce_valid_order_statuses_for_cancel', 'help wp_cancel_orders', 10, 2); function helpwp_cancel_orders ($ statuses, $ order) { $ custom_statuses = array ('completed', 'pending', 'processing', 'on-hold', 'failed'); // States allowed to cancel $ duration = 30; // Maximum age of the order in days 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; }
In the previous code there are 2 lines that you will surely want to modify to suit your needs:
-
$duration = 30;
– This is the age in days of the order so that the customer sees the cancellation button and can easily cancel the order. -
$custom_statuses = array( 'valor' , 'valor');
– Here you must put a value for each order status in which the button will be displayed, and the possibility of canceling the order.
Depending on the limits you define in these fields , the buttons to cancel orders will be displayed or not on the customer’s account page .
Where can i paste this code
You can add this code to various places in your WordPress installation and do it in various ways. In this guide I explain all the possibilities:
How and where to paste PHP, JS, CSS codes and functions that you find out there in WordPress
YOU MAY ALSO BE INTERESTED IN …
Did you like this article? You can’t imagine what you’re missing on YouTube !