Plugin Contributor
Ewout
(@pomegranate)
Hi Eli,
This is a bit beyond the support for the free version indeed, but I think I can help you get in the right direction.
The following code will get an array of all orders for a certain customer. All you need to do is get the first or last result and then use the post id to get the order number…
<?php
global $wpdb;
// we use customer email to also apply this for guest users
$customer_email = get_post_meta($this->order->id,'_billing_email',true);
// filter only completed & processing
$order_statuses = array( 'wc-completed', 'wc-processing' );
$customer_orders = $wpdb->get_results( "SELECT *
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_billing_email'
AND posts.post_type IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
AND posts.post_status IN ('" . implode( "','", (array) $order_statuses ) . "')
AND meta_value = '$customer_email'
" );
}
?>
Hope that helps!
Ewout
Thank you very much for your time! This will definitely get me started. I will post back what I figure out based on the code you’ve provided. This gives me a lot to work with.
Hi Ewout,
I’ve been playing around with this code you provided. So far, I’ve been able to output all the dates from all the orders that the customer has placed in the past – I’m still working on being able to just display the last order.
What I’m having trouble with is this bit of code:
$customer_email = get_post_meta($this->order->id,'_billing_email',true);
When I input a specific customer email rather than pulling the email through the code, everything prints out fine. When I try to pull in the data with the above code, it just breaks the template and shows nothing. I’m definitely learning, so I know I’m not doing something right.
Just FYI, this is what I have so far (that is now pulling just the second to last order instead of all).
This is working, except that the code I mentioned above breaks it. If I input a specific email, it works fine. I’m having trouble pulling in the billing email from the current customer order.
** I’ve pulled bits of code from other sources to make this work, so I know it’s not perfect, and probably not very efficient!
function wc_get_customer_orders() {
global $wpdb;
// we use customer email to also apply this for guest users
$customer_email = '[email protected]';
// filter only completed & processing
$order_statuses = array( 'wc-completed', 'wc-processing' );
$customer_orders = $wpdb->get_results( "SELECT * FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_billing_email'
AND posts.post_type IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
AND posts.post_status IN ('" . implode( "','", (array) $order_statuses ) . "')
AND meta_value = '$customer_email'
ORDER BY id DESC LIMIT 1 OFFSET 1 ;
" );
if ( $customer_orders >=1 ) : ?>
<table class="shop_table my_account_orders" width="100%">
<thead>
<tr>
<th class="order-date"><span class="nobr"><?php _e( 'Last Order Date:', 'woocommerce' ); ?></span></th>
</tr>
</thead>
<tbody><?php
foreach ( $customer_orders as $customer_order ) {
$order = new WC_Order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
?><tr class="order">
<td class="order-date">
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
</td>
</tr><?php
}
?></tbody>
</table>
<?php endif;
}
add_action( 'wpo_wcpdf_after_order_data', 'wc_get_customer_orders', 10, 2 );
Plugin Contributor
Ewout
(@pomegranate)
That explains π ‘$this’ is not defined outside of the plugins context, filters don’t run in a class but just as procedural scripts. I wrote the code as if it were placed in the template itself, when $this refers to the wcpdf export object (which also holds the order object).
Anyway, the wpo_wcpdf_after_order_data filter gets two parameters, which you did’t use: $template_type and $order. So you can get the $order_id from that $order object (if you make sure it reads those parameters):
function wc_get_customer_orders($template_type, $order) {
$customer_email = get_post_meta($order->id,'_billing_email',true);
I haven’t tested your code, but that’ll probably do the trick!
If not, try enabling DEBUG output, as this will give you a more descriptive error.
Ewout
That worked perfectly! I actually had the two parameters set in wpo_wcpdf_after_order_data, however when setting a custom email, it didn’t seem to make a difference.
However, now that I changed both lines of code, it’s working just as expected. Thank you very much for your help on this project…I certainly learned a lot and really appreciate your help and explaining in detail along the way!