When building an e-commerce website, “giving convenience to others is giving convenience to yourself.” When customers find it quick and easy to shop on your site, they are more likely to return and recommend your store to their friends. Adding a “Buy Again” (or reorder) feature to the order list page is an excellent opportunity to improve the customer experience.
This feature is particularly suitable for e-commerce sites selling fast-moving consumer goods (FMCG) or consumables that require frequent repeat purchases. Since WooCommerce doesn’t include a “Buy Again” button by default in the order list, I’ll show you how to implement it in this article.
First, Add the “Buy Again” Button on the Order List Page
First, we need to add a “Buy Again” button to the user’s order list page. This is relatively straightforward using WooCommerce hooks. Simply add the following code to your theme’s functions.php file.
add_filter('woocommerce_my_account_my_orders_actions', 'wprs_add_reorder_button', 10, 2);
function wprs_add_reorder_button($actions, $order) {
// Check if the order status is completed
if ($order->has_status('completed')) {
$actions['reorder'] = array(
'url' => wp_nonce_url(add_query_arg('reorder', $order->get_id()), 'woocommerce-reorder'),
'name' => 'Buy Again'
);
}
return $actions;
}
Process the Reorder Request and Implement the Logic
After the user clicks the “Buy Again” button, a request is submitted to the backend. We need to handle this request in several steps to complete the reorder logic:
- Security Verification: Confirm the request is legitimate using nonces.
- Retrieve Original Order: Use the
reorderparameter (Order ID) to fetch the source order data. - Clear Current Cart: Optional step depending on whether you want to replace current cart items or append to them.
- Add Products to Cart: Before adding, verify if the products are still available and in stock. If in stock, add them; otherwise, notify the user.
- Redirect: Once processed, redirect the user to the cart page for checkout.
add_action('wp_loaded', 'wprs_handle_reorder_request', 20);
function wprs_handle_reorder_request() {
if (!isset($_GET['reorder']) || !is_numeric($_GET['reorder'])) {
return;
}
// Verify nonce
if (!wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-reorder')) {
wc_add_notice('Invalid request', 'error');
wp_safe_redirect(wc_get_page_permalink('myaccount'));
exit;
}
// Get original order
$order_id = absint($_GET['reorder']);
$order = wc_get_order($order_id);
if (!$order) {
wc_add_notice('Order not found', 'error');
wp_safe_redirect(wc_get_page_permalink('myaccount'));
exit;
}
// Empty current cart
WC()->cart->empty_cart();
// Add items from the order to cart
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$quantity = $item->get_quantity();
// Check if product is still purchasable and in stock
$product = wc_get_product($product_id);
if (!$product || !$product->is_purchasable() || !$product->is_in_stock()) {
wc_add_notice(sprintf('Product "%s" is no longer available or out of stock', $item->get_name()), 'error');
continue;
}
// Add to cart
if ($variation_id) {
WC()->cart->add_to_cart($product_id, $quantity, $variation_id);
} else {
WC()->cart->add_to_cart($product_id, $quantity);
}
}
// Redirect to cart page
wp_safe_redirect(wc_get_cart_url());
exit;
}
Implement “Buy Again” via Plugins
If you prefer not to use custom code, several plugins can help you achieve the same functionality. Here are two solid options worth trying:
