Efficient order management is crucial for any e-commerce store. In this snippet, we’ll explore how to enhance the order management experience by adding a “Cancel Order” button alongside the “View Order” button on the order history tab in WooCommerce. This feature allows customers to easily cancel orders directly from their order history.
/**
* Snippet Name: Change cancel order button
* Snippet Author: wpsnippets.sev
*/
//Filter valid order statuses for cancel to include custom statuses
function wps_filter_valid_order_statuses_for_cancel( $statuses, $order ) {
$custom_statuses = array( 'pending', 'processing', 'on-hold', 'failed' );
return $custom_statuses;
}
add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'wps_filter_valid_order_statuses_for_cancel', 20, 2 );
Code Explanation
- The woocommerce_valid_order_statuses_for_cancel filter hook is used to modify the list of valid order statuses for cancellation.
- By default, WooCommerce allows cancellation for certain order statuses like “pending,” “processing,” “on hold,” etc. This snippet extends the list to include custom statuses such as “failed,” ensuring more flexibility in order cancellation.