If you’re running a WooCommerce store, you know how important product reviews are for building trust and boosting sales. But as your product catalog grows, it can become hard for customers to keep track of which purchases still need reviews.
That’s where a Purchase History tab comes in handy. Imagine offering your customers a simple way to see all their previous purchases, with a clear indicator of which items they haven’t reviewed yet.
In this tutorial, we’ll show you how to create a custom Purchase History tab within the My Account page. This tab will list purchased products, the date of purchase, and provide a quick “Review” link if the product hasn’t been reviewed yet.
Itβs a small feature that can make a big impact on your storeβs reviews and engagement. Keep reading to find the code snippet that will make it happen!
PHP Snippet 1 of 2: Custom “Purchase History” Tab @ WooCommerce My Account
First of all, let’s create a custom tab. I’m using my “Add a custom My Account tab” tutorial for this.
Note: resave / refresh your permalinks once you use the code below, otherwise the tab will give you 404 error.
/**
* @snippet Purchase History Tab | WooCommerce My Account
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 9
* @community https://businessbloomer.com/club/
*/
add_action( 'init', 'bbloomer_add_purchase_history_endpoint' );
function bbloomer_add_purchase_history_endpoint() {
add_rewrite_endpoint( 'purchase-history', EP_ROOT | EP_PAGES );
}
add_filter( 'query_vars', 'bbloomer_purchase_history_query_vars', 0 );
function bbloomer_purchase_history_query_vars( $vars ) {
$vars[] = 'purchase-history';
return $vars;
}
add_filter( 'woocommerce_get_query_vars', 'bbloomer_add_wc_query_vars' );
function bbloomer_add_wc_query_vars( $vars ) {
$vars['purchase-history'] = 'purchase-history';
return $vars;
}
Also, let’s give it a page title:
add_filter( 'woocommerce_endpoint_purchase-history_title', 'bbloomer_purchase_history_title' );
function bbloomer_purchase_history_title( $title ) {
return 'Purchase History';
}
Here’s the final result:
PHP Snippet 2 of 2: “Purchase History” Tab Content
Now let’s populate the tab with the list of purchased items by the current user. Each item will get a series of custom actions, and one of these may be the “Leave a Review” button in case they haven’t left a review yet!
/**
* @snippet Purchase History Tab Content | WooCommerce My Account
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 9
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_account_purchase-history_endpoint', 'bbloomer_display_purchase_history' );
function bbloomer_display_purchase_history() {
$customer_orders = wc_get_orders( array(
'customer_id' => get_current_user_id(),
'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
));
$customer = new WC_Customer( get_current_user_id() );
$reviewed_products = [];
$comments = get_comments( array(
'author_email' => $customer->get_billing_email(),
'type' => 'review',
'status' => 'approve',
));
foreach ( $comments as $comment ) {
$reviewed_products[] = $comment->comment_post_ID;
}
if ( $customer_orders ) {
echo '<table class="woocommerce-table shop_table shop_table_responsive">';
echo '<thead><tr><th>Product</th><th>Date</th><th>Actions</th></tr></thead><tbody>';
foreach ( $customer_orders as $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
echo '<tr>';
echo '<td><a href="' . esc_url( $product->get_permalink() ) . '">' . esc_html( $item->get_name() ) . '</a></td>';
echo '<td>' . esc_html( wc_format_datetime( $order->get_date_created() ) ) . '</td>';
echo '<td>';
if ( ! in_array( $product->get_id(), $reviewed_products ) ) {
echo '<a class="button alt" href="' . esc_url( get_permalink( $product->get_id() ) . '#tab-reviews' ) . '">' . esc_html__( 'Add a review', 'woocommerce' ) . '</a> ';
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody></table>';
} else {
echo '<p>No purchases found.</p>';
}
}
And this is how it will look:
In case the current user already reviewed a given product, the button won’t show. You can of course add other custom actions, such as “Share with a friend”, “Order again”, etc.