Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes
CustomerHistory::get_customer_history
Get the order history for the customer.
Method of the class: CustomerHistory{}
No Hooks.
Returns
Array{orders_count:. int, total_spend: float, avg_order_value: float, tooltip: string} Order count, total spend, average order value, and tooltip text.
Usage
// private - for code of main (parent) class only $result = $this->get_customer_history( $order ): array;
- $order(WC_Order) (required)
- The order object.
CustomerHistory::get_customer_history() CustomerHistory::get customer history code WC 10.8.1
private function get_customer_history( WC_Order $order ): array {
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$customer_id = $order->get_customer_id();
$billing_email = $order->get_billing_email();
$result = $this->query_hpos( $customer_id, $billing_email );
} elseif ( method_exists( $order, 'get_report_customer_id' ) ) {
$result = $this->query_cpt( $order->get_report_customer_id() );
} else {
wc_get_logger()->warning(
'CustomerHistory: Order object does not have get_report_customer_id method.',
array( 'source' => 'customer-history' )
);
$result = (object) array(
'orders_count' => 0,
'total_spend' => 0,
);
}
$orders_count = (int) ( $result->orders_count ?? 0 );
$total_spend = (float) ( $result->total_spend ?? 0 );
// Build a dynamic tooltip listing the excluded statuses by their translated labels.
// Internal statuses (auto-draft, trash) are naturally filtered out because they
// don't exist in wc_get_order_statuses(). checkout-draft is skipped explicitly
// because it is force-excluded by DraftOrders but is not a configurable option
// on the Analytics settings page, so it would be confusing to surface it here.
$all_statuses = wc_get_order_statuses();
$excluded_labels = array();
foreach ( $this->get_excluded_statuses() as $slug ) {
if ( 'checkout-draft' === $slug ) {
continue;
}
$prefixed = 'wc-' . $slug;
if ( isset( $all_statuses[ $prefixed ] ) ) {
$excluded_labels[] = mb_strtolower( $all_statuses[ $prefixed ] );
}
}
if ( ! empty( $excluded_labels ) ) {
$tooltip = sprintf(
/* translators: %s: localized list of order status names, e.g. "pending payment, failed, and cancelled" */
__( 'Total number of orders for this customer, excluding %s orders, including the current one.', 'woocommerce' ),
wp_sprintf_l( '%l', $excluded_labels )
);
} else {
$tooltip = __( 'Total number of orders for this customer, including the current one.', 'woocommerce' );
}
return array(
'orders_count' => $orders_count,
'total_spend' => $total_spend,
'avg_order_value' => $orders_count > 0 ? $total_spend / $orders_count : 0,
'tooltip' => $tooltip,
);
}