Home » WooCommerce: Order Again Button @ My Account > Orders

WooCommerce: Order Again Button @ My Account > Orders

by adminkys

The WooCommerce “Order Again” button displays for ‘completed’ orders on the Thank You page and View Order page. That’s a pity, because it would be useful to show it on the My Account > Orders page as well, as a custom “action”, same as the “View”, “Pay” (if pending), “Cancel” (if subscription), “Edit” (custom snippet), “Confirm” (custom snippet) buttons.

The good news is that we can code it ourselves! And just reuse most of the code we already wrote, as well as rely on the WooCommerce “listener” for the existing “Order Again” button. Enjoy!

Completed orders will now get a custom “Order again” button under My Account > Orders. On click, the Cart will be filled out with the same products that were in the original order.

PHP Snippet: Add “Order Again” Button to My Account > Orders Action (Completed Orders Only)

If you’re wondering how I came up with the ‘url’ code in the snippet below, I simply reused the exact same code of the woocommerce_order_again_button() function.

Also, somewhere in the Cart class, there is a “listener” that triggers when the button is clicked – see get_cart_from_session() – so because I’m using the exact same button URL, the listener triggers from the new button position as well.

/**
 * @snippet       Order Again @ My Account Orders
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'woocommerce_my_account_my_orders_actions', 'tutoraspire_order_again_action', 9999, 2 );
   
function tutoraspire_order_again_action( $actions, $order ) {
    if ( $order->has_status( 'completed' ) ) {
        $actions['order-again'] = array(
            'url' => wp_nonce_url( add_query_arg( 'order_again', $order->get_id(), wc_get_cart_url() ), 'woocommerce-order_again' ),
            'name' => __( 'Order again', 'woocommerce' ),
        );
    }
    return $actions;
}

You may also like