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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* 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( '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href="%s" title="%s" target="_blank">%s</a></td></tr>', $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 = '<table id="viewCartTable"> <thead> <tr> <th>Order Number</th> <th>Date</th> <th>Total</th> <th>Order Status</th> <th>Receipt</th> </tr> </thead> <tbody>' . $data . '</tbody></table>'; return $table; } add_shortcode( 'order_history', 'cart66_order_history' ); |












Nancy Nardi 12:02 pm on April 3, 2012 Permalink |
This made my day. I have been trying to figure out a way to add an order history to the Cart66 account page – and this was a very easy fix. Thank you!
Frankie Jarrett 12:09 pm on April 3, 2012 Permalink |
You’re welcome, Nancy! Glad this helped you out.
zach 5:10 pm on April 18, 2012 Permalink |
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.
Frankie Jarrett 5:14 pm on April 18, 2012 Permalink |
Hey Zach, it could be that this shortcode only works with products that create memberships.
zach 5:48 pm on April 18, 2012 Permalink |
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?
Soul Sanctuary 9:14 pm on July 4, 2012 Permalink |
I would also like to know if it is possible to alter this to work on users emails instead of account ID’s Personally I have uses for both version.
Kind Regards
Luke
Farzam 7:10 am on July 30, 2012 Permalink |
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
Kieran 3:21 am on October 12, 2012 Permalink |
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
Keven 12:54 pm on October 20, 2012 Permalink |
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!