WC_Customer_Data_Store::get_last_order( WC_Customer $customer )

Gets the customers last order.


Description Description


Parameters Parameters

$customer

(Required) Customer object.


Top ↑

Return Return

(WC_Order|false)


Top ↑

Source Source

File: includes/data-stores/class-wc-customer-data-store.php

	public function get_last_order( &$customer ) {
		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
		);

		if ( ! $last_order ) {
			return false;
		}

		return wc_get_order( absint( $last_order ) );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.