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' );