WordPress at Your Fingertips
function is not described

WC_Customer_Data_Store::get_last_order() public WC 3.0.0

Gets the customers last order.

{} It's a method of the class: WC_Customer_Data_Store{}

Hooks from the method
Return

WC_Order|false.

Usage

$WC_Customer_Data_Store = new WC_Customer_Data_Store();
$WC_Customer_Data_Store->get_last_order( $customer );
$customer(WC_Customer) (required) (passed by reference — &)
Customer object.

Changelog

Since 3.0.0 Introduced.

Code of WC_Customer_Data_Store::get_last_order() WC 5.6.0

public function get_last_order( &$customer ) {
	$last_order = apply_filters(
		'woocommerce_customer_get_last_order',
		get_user_meta( $customer->get_id(), '_last_order', true ),
		$customer
	);

	if ( '' === $last_order ) {
		global $wpdb;

		$last_order = $wpdb->get_var(
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			"SELECT posts.ID
			FROM $wpdb->posts AS posts
			LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
			WHERE meta.meta_key = '_customer_user'
			AND   meta.meta_value = '" . esc_sql( $customer->get_id() ) . "'
			AND   posts.post_type = 'shop_order'
			AND   posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
			ORDER BY posts.ID DESC"
			// phpcs:enable
		);
		update_user_meta( $customer->get_id(), '_last_order', $last_order );
	}

	if ( ! $last_order ) {
		return false;
	}

	return wc_get_order( absint( $last_order ) );
}