Create a shortcode for displaying Order History on your Cart66 site

I’m a fan of Cart66. It’s easy to integrate and has a fair amount of features that make it a flexible e-commerce and member management solution for WordPress.

However, there’s one pretty big feature that Cart66 is missing: the ability to display the Order History of a logged in user.

What? Really?

Yes. Really.

This doesn’t really require a lot of explanation. It was easy to achieve with a simple SQL query and a custom loop. And all without editing the Cart66 core! So you can keep updating the plugin without worry. 🙂

Websites that sell digital goods will especially find this handy since users will be able to view their past receipts to re-download purchases.

Just insert this code into the functions.php file of your WordPress theme and use the shortcode [order_history] on a member’s only page to show the Order History for that user. Enjoy!

The original code for this idea came from a post by Alison Barrett.

/* Shortcode for displaying Cart66 order history for the current logged in user
 *
 * @return {string} $table
 * @shortcode order_history
 */
function cart66_order_history( $atts, $content = null ) {
	extract( shortcode_atts( array(), $atts ) );
	global $wpdb;
	$results = $wpdb->get_results( "SELECT ouid, ordered_on, trans_id, total, status FROM " . $wpdb->prefix . "cart66_orders WHERE account_id = " . Cart66Session::get( 'Cart66AccountId' ) . ' ORDER BY ordered_on DESC' );
	foreach ( $results as $order ) {
		$data .= sprintf( '%s%s%s%s%s', $order->trans_id, date( 'F j, Y', strtotime( $order->ordered_on ) ), $order->total, ucwords( $order->status ), home_url( '/store/receipt/?ouid=' . $order->ouid ), __( 'Click to view receipt', 'cart66' ), __( 'View Receipt', 'cart66' ) );
	}
	$table = '' . $data . '
Order Number Date Total Order Status Receipt
'; return $table; } add_shortcode( 'order_history', 'cart66_order_history' );
  1. Hey. I’ve been trying your shortcode. Is it dependent of a specific version of Cart66? I’ve been testing it out, and I can’t get any orders i’ve placed to display in the history.

  2. Thank you for your quick response!

    I’ve been struggling with it, and I can’t seem to find how to set products to create membership (i’d much rather not to force membership on those who don’t want it also), is it possible in any way to edit this short-code to query the sql based on a logged in users email, rather than account name?

  3. Hi it is great post, although if possible I want to know about all the fields exist in the orders table I want to match the first rowid or the system id field which starts from 1,2,3 and goes on …
    there is a field [ouid] which is the long order id for clients but i am looking for the small order record no. just wana know the field name.
    the main aim is when i click on the view receipt it take me to the order where i can edit it or mark it as complete ////

    thanks for reply

  4. Hi,
    great post, very handy tool, was just wondering if there was a way of modifying it so that it lists orders from a specific feature level instead of just an account?

    Regards,

    Kieran

  5. Hi there

    I am a novice php guy, so some experience but not much. I copied the code you provided at the VERY end of my functions.php file, and the site would not load at all. I had to ftp the file down, remove the code, and reload it to the directory.
    Can you offer any advice, on specifically WHERE the code should be placed? thanks again!

  6. Hi Frankie! I’m Lee Blue, the founder of Cart66. We’ve made a great deal of updates since this article was written. We have an entire, secure, customer portal now for Cart66 where your customers can log in and see order history, manage subscriptions, save billing and shipping info for repeat orders, etc. We’ve got a bunch of new videos on our site showing how to do all sorts of cool stuff. Cart66 even handles your PCI compliance needs now by providing a secure hosted payment page skinned with your WordPress theme so you get all the security benefits of a hosted payment page while also keeping all of your design in place. If you haven’t stopped by lately, please come check us out. I’d love to help answer any questions.

  7. hi I use your code but error occur could not find session cart66 in my site what is solution of this.please reply me as soon as possible.

Leave a Reply

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

Up Next:

What's the future of the WordPress iOS app?

What's the future of the WordPress iOS app?