2
votes

In Woocommerce My account Orders, I am using the following code to Check if customer orders are in "processing" or "shipped" status and if it is the case, display a button to cancel the order:

    if ($order_status == 'processing' || $order_status == 'shipped' ){
    echo '<a href="click=1" class="woocommerce-button button return">Return Order</a>';
        $order->update_status('cancelled');
}

With my code, the order get automatically updated to cancel status when the status was "processing" or "shipped" and I can't get the button to work. I want to make that happen on button click to trigger the update_status. I want to make it appear in view order page like this enter image description here Right now the cancel order isn't functional How can I do? without using Javascript?

Any help is welcome.

1

1 Answers

3
votes

The following code will enable the Woocommerce "cancel" cation button in My account orders list, also for processing or shipped order statuses (default Woocommerce statuses are here pending and failed):

add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel', 10, 1 );
function custom_valid_order_statuses_for_cancel( $statuses ){

    // Set HERE the order statuses where you want the cancel button to appear
    return array_merge( $statuses, array('processing', 'shipped'));
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.