Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Allow Customers to Edit Orders@ WooCommerce My Account Page?

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.

WooCommerce My Account Page

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x