By default, WooCommerce lacks a feature allowing customers to edit their orders post-purchase. In this post, we present a solution: a functionality alike adding an ‘Order Again’ button in WooCommerce on My Account Orders page . As the order again feature is available only for ‘completed’ orders, here we’re going one step beyond that! We’re adding an ‘Edit Order’ button, displayed just for orders that are still being processed.
Solution: Allow Customers to Edit Order @My Account Orders Page
The code snippet will allow customers to edit their orders which are in processing status.
add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'ts_order_again_statuses' ); function ts_order_again_statuses( $statuses ) { $statuses[] = 'processing'; return $statuses; } // Add 'Edit Order' Action in My Account Orders add_filter( 'woocommerce_my_account_my_orders_actions', 'ts_add_edit_order_my_account_orders_actions', 50, 2 ); function ts_add_edit_order_my_account_orders_actions( $actions, $order ) { // Add 'Edit Order' action only for orders with 'processing' status if ( $order->has_status( 'processing' ) ) { $actions['edit-order'] = array( 'url' => wp_nonce_url( add_query_arg( array( 'order_again' => $order->get_id(), 'edit_order' => $order->get_id() ) ), 'woocommerce-order_again' ), 'name' => __( 'Edit Order', 'woocommerce' ) ); } return $actions; } // Detect and Store 'Edit Order' Action in Session add_action( 'woocommerce_cart_loaded_from_session', 'ts_detect_edit_order' ); function ts_detect_edit_order( $cart ) { // Detect 'Edit Order' action and store order ID in session if ( isset( $_GET['edit_order'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-order_again' ) ) { WC()->session->set( 'edit_order', absint( $_GET['edit_order'] ) ); } } // Save 'Edit Order' Action if New Order is Placed add_action( 'woocommerce_checkout_update_order_meta', 'ts_save_edited_order_action' ); function ts_save_edited_order_action( $order_id ) { $edited_order_id = WC()->session->get( 'edit_order' ); if ( ! empty( $edited_order_id ) ) { // Update new order with reference to edited order update_post_meta( $order_id, '_edit_order', $edited_order_id ); $old_order = new WC_Order( $edited_order_id ); $old_order->update_status( 'cancelled', 'Order cancelled after editing. New order: ' . $order_id ); // Clear session data WC()->session->set( 'edit_order', null ); } }
Output
The ‘Edit Order’ button is added to the list of actions exclusively for processing order status displayed in the customer’s account page of the orders section. When clicked, customers are redirected to their cart with their previous items loaded. Any changes made to the cart or other details are saved, creating a new edited order. Once the edited order is placed, the original order is automatically cancelled.
Alternatively, you can also add a cancel button for certain WooCommerce Order status. This grants customers with the flexibility to cancel their orders and have control over their shopping.