Celebrate CodingBunny’s founder turning 45 - 45% OFF
The offer will end in
DAYS
HOURS
MINUTES
Adds "Buy Again" button to WooCommerce completed orders in My Account.

/**
* Snippet Name: Add Buy Again Button + Purchased Notice on Single Product
* Snippet Author: coding-bunny.com
* Description: Adds "Buy Again" button to WooCommerce completed orders in My Account.
* Version: 1.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'cbbab_add_buy_again_button', 50, 2 );
function cbbab_add_buy_again_button( $actions, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
return $actions;
}
if ( $order->has_status( 'completed' ) ) {
$order_id = $order->get_id();
$url = add_query_arg(
array(
'cbbab_buy_again' => absint( $order_id ),
),
wc_get_account_endpoint_url( 'orders' )
);
$url = wp_nonce_url( $url, 'cbbab_order_again' );
$actions['cbbab-buy-again'] = array(
'url' => esc_url( $url ),
'name' => esc_html__( 'Buy Again', 'woocommerce' ),
'class' => 'cbbab-buy-again-button',
);
}
return $actions;
}
add_action( 'template_redirect', 'cbbab_handle_buy_again_request', 20 );
function cbbab_handle_buy_again_request() {
if (
is_account_page() &&
isset( $_GET['cbbab_buy_again'] ) &&
! empty( $_GET['cbbab_buy_again'] )
) {
$order_id = absint( $_GET['cbbab_buy_again'] );
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'cbbab_order_again' ) ) {
wc_add_notice( esc_html__( 'Action not allowed, please try again.', 'woocommerce' ), 'error' );
return;
}
$order = wc_get_order( $order_id );
if (
! $order ||
get_current_user_id() !== $order->get_user_id()
) {
wc_add_notice( esc_html__( 'Order not valid or not authorized.', 'woocommerce' ), 'error' );
return;
}
$added = false;
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$quantity = $item->get_quantity();
$product = $item->get_product();
$item_data = array();
if ( ! $product || ! $product->is_purchasable() ) {
continue;
}
if ( $variation_id && $item->get_meta_data() ) {
foreach ( $item->get_meta_data() as $meta ) {
if ( taxonomy_exists( $meta->key ) ) {
$item_data[ $meta->key ] = $meta->value;
}
}
}
$added = true;
WC()->cart->add_to_cart(
$product_id,
$quantity,
$variation_id,
$item_data
);
}
if ( $added ) {
wc_add_notice( esc_html__( 'Products added to your cart.', 'woocommerce' ), 'success' );
wp_safe_redirect( wc_get_cart_url() );
exit;
} else {
wc_add_notice( esc_html__( 'Could not add the products to your cart.', 'woocommerce' ), 'error' );
}
}
}
add_action( 'woocommerce_before_single_product', 'cbbab_show_purchased_notice_on_product_page' );
function cbbab_show_purchased_notice_on_product_page() {
if ( ! is_user_logged_in() || ! is_product() ) {
return;
}
global $product;
if ( ! $product || ! $product->get_id() ) {
return;
}
$customer_id = get_current_user_id();
$product_id = $product->get_id();
$customer_orders = wc_get_orders( array(
'customer_id' => $customer_id,
'status' => 'completed',
'limit' => -1,
'return' => 'objects',
) );
$order_link = '';
foreach ( $customer_orders as $order ) {
foreach ( $order->get_items() as $item ) {
if ( $item->get_product_id() == $product_id || $item->get_variation_id() == $product_id ) {
if ( function_exists( 'wc_get_account_view_order_url' ) ) {
$order_link = esc_url( wc_get_account_view_order_url( $order->get_id() ) );
} else {
$order_link = esc_url( add_query_arg( 'view-order', $order->get_id(), wc_get_account_endpoint_url( 'orders' ) ) );
}
break 2;
}
}
}
if ( $order_link ) {
$message = sprintf(
esc_html__( 'You have previously purchased this product. %1$sView your order%2$s.', 'woocommerce' ),
'<a href="' . $order_link . '">',
'</a>'
);
wc_print_notice( $message, 'notice' );
}
}.cbbab-buy-again {
background-color: #ff6b35;
border-color: #ff6b35;
}
.cbbab-buy-again:hover {
background-color: #e55a2b;
border-color: #e55a2b;
}