Offering customers the convenience of reordering products from their previous orders can enhance user experience and encourage repeat purchases. In this snippet, we’ll learn how to add an “Order Again” button to WooCommerce, allowing customers to quickly reorder items from their order history.
/**
* Snippet Name: Add an order again button to order history
* Snippet Author: wpsnippets.dev
*/
//Add "Order Again" button to the order history tab in My Account
function wps_add_order_again_button( $actions, $order ) {
// Check if order status is eligible for reorder
if ( in_array( $order->get_status(), array( 'completed') ) ) {
$actions['order-again'] = array(
'url' => wp_nonce_url( add_query_arg( 'order_again', $order->get_id() ), 'woocommerce-order_again' ),
'name' => __( 'Order Again', 'textdomain' )
);
}
return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'wps_add_order_again_button', 10, 2 );
Code Explanation
- This snippet adds an “Order Again” button to each order listed in the order history tab of the “My Account” page.
- The button is only displayed for orders with the status “completed.”
- When the “Order Again” button is clicked, the associated order items are added to the cart, and the customer is redirected to the cart page to complete the purchase.