Automattic\WooCommerce\Admin\API\Reports\Customers
DataStore::get_last_order
Retrieve the last order made by a customer.
Method of the class: DataStore{}
No Hooks.
Returns
Object. WC_Order|false.
Usage
$result = DataStore::get_last_order( $customer_id );
- $customer_id(int) (required)
- Customer ID.
DataStore::get_last_order() DataStore::get last order code WC 10.8.1
public static function get_last_order( $customer_id ) {
global $wpdb;
$orders_table = $wpdb->prefix . 'wc_order_stats';
$last_order = $wpdb->get_var(
$wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT order_id, date_created_gmt FROM {$orders_table}
WHERE customer_id = %d
ORDER BY date_created_gmt DESC, order_id DESC LIMIT 1",
// phpcs:enable
$customer_id
)
);
if ( ! $last_order ) {
return false;
}
return wc_get_order( absint( $last_order ) );
}