WooCommerce Show Previous Orders Of Product On Single Product Template

WooCommerce product showing order history


There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

This snippet allows logged in users to see a history of previous purchases of the currently viewed product under the add to cart button. This snippet is particularly useful to remind customers of when they have previously purchased this product. The date of the purchase is hyperlinked to the order view page. You’ll also notice the quantity of the products ordered is appended as a suffix to the link. Keep in mind, this will only the order history if the status of the order is set to complete. If you are finding previous product orders are not showing, it may be the order status you need to look at.

 * Snippet Name:	WooCommerce Show Previous Orders Of Product On Single Product Template
 * Snippet Author:	ecommercehints.com

add_action( 'woocommerce_after_add_to_cart_form', 'ecommercehints_show_product_order_history', 10 );
function ecommercehints_show_product_order_history() {
	global $product, $post;
	echo '<strong>Previous Purchases</strong><br><ol>';
	$orders = get_posts( array(
		'numberposts' => -1,
		'meta_key'    => '_customer_user',
		'meta_value'  => get_current_user_id(),
		'post_type'   => 'shop_order',
		'post_status' => 'wc-completed'
	) );
	foreach ($orders as $order) {
		$order = new WC_Order( $order->ID );
		$order_data = $order->get_data();
		$public_view_order_url = esc_url( $order->get_view_order_url() );
		$items = $order->get_items();
		foreach( $items as $item ) {
			$product_id = $item['product_id'];
			$quantity = $item->get_quantity();
			if ( $post->ID == $product_id ) { 
				echo '<li><a href="' . $public_view_order_url . '">' . $order_date . ' &times;</a>' . $quantity . '</li>'; 
	echo '</ol></div>';

Snippet Benefits

  • Let the customer see their order history of the currently viewed product.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers