"Buy Again" Tab With Previously Purchased Products
Adds a My Account tab listing every product the customer has bought before, each with an add-to-cart button, ideal for consumables and repeat orders.
Opens WPIDE on
Code
$wpide_buy_again_slug = 'buy-again'; // URL slug: /my-account/buy-again/
add_action( 'init', function () use ( $wpide_buy_again_slug ) {
add_rewrite_endpoint( $wpide_buy_again_slug, EP_ROOT | EP_PAGES );
} );
add_filter( 'woocommerce_get_query_vars', function ( $vars ) use ( $wpide_buy_again_slug ) {
$vars[ $wpide_buy_again_slug ] = $wpide_buy_again_slug;
return $vars;
} );
add_filter( 'woocommerce_account_menu_items', function ( $items ) use ( $wpide_buy_again_slug ) {
$new = array();
foreach ( $items as $key => $label ) {
$new[ $key ] = $label;
if ( 'orders' === $key ) {
$new[ $wpide_buy_again_slug ] = 'Buy again';
}
}
return $new;
} );
add_action( 'woocommerce_account_' . $wpide_buy_again_slug . '_endpoint', function () {
$user_id = get_current_user_id();
$ids = get_transient( 'wpide_buy_again_' . $user_id );
if ( false === $ids ) {
$ids = array();
$orders = wc_get_orders( array( 'customer_id' => $user_id, 'status' => wc_get_is_paid_statuses(), 'limit' => 50 ) );
foreach ( $orders as $order ) {
foreach ( $order->get_items() as $item ) {
$ids[] = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
}
}
$ids = array_values( array_unique( array_filter( $ids ) ) );
set_transient( 'wpide_buy_again_' . $user_id, $ids, DAY_IN_SECONDS );
}
$shown = 0;
echo '<ul class="wpide-buy-again" style="list-style:none;margin:0;padding:0">';
foreach ( $ids as $id ) {
$product = wc_get_product( $id );
if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) {
continue;
}
$shown++;
printf(
'<li style="display:flex;gap:1em;align-items:center;padding:.75em 0;border-bottom:1px solid #eee"><a href="%1$s" style="flex:0 0 64px">%2$s</a><span style="flex:1"><a href="%1$s">%3$s</a><br>%4$s</span><a class="button" href="%5$s">%6$s</a></li>',
esc_url( $product->get_permalink() ),
wp_kses_post( $product->get_image( array( 64, 64 ) ) ),
esc_html( $product->get_name() ),
wp_kses_post( $product->get_price_html() ),
esc_url( $product->is_type( 'variation' ) ? add_query_arg( array_merge( array( 'add-to-cart' => $product->get_parent_id(), 'variation_id' => $id ), $product->get_variation_attributes() ), wc_get_cart_url() ) : add_query_arg( 'add-to-cart', $id, wc_get_cart_url() ) ),
esc_html( 'Add to cart' )
);
}
echo '</ul>';
if ( ! $shown ) {
echo '<p>' . esc_html( 'Products you buy will appear here for quick reordering.' ) . '</p>';
}
} );
add_action( 'woocommerce_order_status_changed', function ( $order_id, $from, $to, $order ) {
if ( $order instanceof WC_Order && $order->get_customer_id() ) {
delete_transient( 'wpide_buy_again_' . $order->get_customer_id() );
}
}, 10, 4 );
Notes
Visit Settings > Permalinks once after activating so the new tab's URL works.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.