WC_Customer_Data_Store::get_order_count()publicWC 3.0.0

Return the number of orders this customer has.

Метод класса: WC_Customer_Data_Store{}

Хуки из метода

Возвращает

Int.

Использование

$WC_Customer_Data_Store = new WC_Customer_Data_Store();
$WC_Customer_Data_Store->get_order_count( $customer );
$customer(WC_Customer) (обязательный) (передается по ссылке — &)
Customer object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Customer_Data_Store::get_order_count() WC 7.3.0

public function get_order_count( &$customer ) {
	$count = apply_filters(
		'woocommerce_customer_get_order_count',
		get_user_meta( $customer->get_id(), '_order_count', true ),
		$customer
	);

	$order_statuses_sql = "( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )";

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

		//phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		if ( $this->is_cot_in_use() ) {
			$sql   = $wpdb->prepare(
				'SELECT COUNT(id) FROM ' . OrdersTableDataStore::get_orders_table_name() . "
				WHERE customer_id = %d
				AND status in $order_statuses_sql",
				$customer->get_id()
			);
			$count = $wpdb->get_var( $sql );
		} else {
			$count = $wpdb->get_var(
				"SELECT COUNT(*)
			FROM $wpdb->posts as posts
			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
			WHERE   meta.meta_key = '_customer_user'
			AND     posts.post_type = 'shop_order'
			AND     posts.post_status IN $order_statuses_sql
			AND     meta_value = '" . esc_sql( $customer->get_id() ) . "'"
			);
		}
		//phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		update_user_meta( $customer->get_id(), '_order_count', $count );
	}

	return absint( $count );
}