If you’re running a WooCommerce store, giving users a quick way to reorder their past purchases can improve their shopping experience—especially for repeat buyers. In this tutorial, we’ll build a simple “Reorder” feature that adds all products from a past order back into the cart using AJAX.
Step 1: Add the “Reorder This” Button
This will add a new action button next to each order in the My Account → Orders list.
add_filter( 'woocommerce_my_account_my_orders_actions', 'custom_add_quick_reorder_button', 10, 2 );
function custom_add_quick_reorder_button( $actions, $order ) {
$actions['quick_reorder'] = array(
'url' => '#',
'name' => __( 'Reorder', 'your-textdomain' ),
'data-order-id' => $order->get_id(),
);
return $actions;
}
Step 2: Handle the Reorder via AJAX
This code will loop through the order items and add them back to the cart.
add_action( 'wp_ajax_quick_reorder_order', 'handle_quick_reorder_order' );
add_action( 'wp_ajax_nopriv_quick_reorder_order', 'handle_quick_reorder_order' );
function handle_quick_reorder_order() {
if ( ! is_user_logged_in() || empty($_POST['order_id']) ) {
wp_send_json_error('Invalid request');
}
$order_id = absint($_POST['order_id']);
$order = wc_get_order( $order_id );
if ( ! $order || $order->get_user_id() !== get_current_user_id() ) {
wp_send_json_error('Unauthorized');
}
foreach ( $order->get_items() as $item ) {
WC()->cart->add_to_cart( $item->get_product_id(), $item->get_quantity() );
}
wp_send_json_success('Products added to cart');
}
Step 3: Enqueue JavaScript
This script will catch the button click and trigger the AJAX request.
add_action('wp_enqueue_scripts', 'enqueue_quick_reorder_script');
function enqueue_quick_reorder_script() {
wp_add_inline_script('jquery', "
jQuery(document).on('click', 'a[data-order-id]', function(e) {
e.preventDefault();
var orderId = jQuery(this).data('order-id');
jQuery.ajax({
url: '".admin_url('admin-ajax.php')."',
method: 'POST',
data: {
action: 'quick_reorder_order',
order_id: orderId,
},
success: function(res) {
if (res.success) {
alert('Reordered successfully!');
window.location.href = '".wc_get_cart_url()."';
} else {
alert('Error: ' + res.data);
}
}
});
});
");
}
Result
Now, when users visit My Account → Orders, they’ll see a “Reorder” button next to each order. Clicking it will automatically add those items back into the cart and redirect to the cart page.