wc_get_customer_last_order

Get info about customer's last order.

Description

(WC_Order) wc_get_customer_last_order( (int) $customer_id ); 

Returns (WC_Order)

Order object if successful or false.

Parameters (1)

0. $customer_id (int)
The customer id.

Usage

  1. if ( !function_exists( 'wc_get_customer_last_order' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php'; 
  3.  
  4. // The customer id. 
  5. $customer_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_get_customer_last_order($customer_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-user-functions.php  
  1. function wc_get_customer_last_order( $customer_id ) { 
  2. global $wpdb; 
  3.  
  4. $customer_id = absint( $customer_id ); 
  5.  
  6. $id = $wpdb->get_var( "SELECT id 
  7. FROM $wpdb->posts AS posts 
  8. LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id 
  9. WHERE meta.meta_key = '_customer_user' 
  10. AND meta.meta_value = {$customer_id} 
  11. AND posts.post_type = 'shop_order' 
  12. AND posts.post_status IN ( '" . implode( "', '", array_keys( wc_get_order_statuses() ) ) . "' ) 
  13. ORDER BY posts.ID DESC 
  14. " ); 
  15.  
  16. return wc_get_order( $id );