Product Page

WooCommerce Show Previous Orders Of Product On Single Product Template

WooCommerce product showing order history

Pre-Requisites

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();
		$order->get_date_completed();
		$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.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER