WC_Customer_Data_Store::get_order_count()
Return the number of orders this customer has.
Method of the class: WC_Customer_Data_Store{}
Hooks from the method
Return
Int
.
Usage
$WC_Customer_Data_Store = new WC_Customer_Data_Store(); $WC_Customer_Data_Store->get_order_count( $customer );
- $customer(WC_Customer) (required) (passed by reference — &)
- Customer object.
Changelog
Since 3.0.0 | Introduced. |
WC_Customer_Data_Store::get_order_count() WC Customer Data Store::get order count code WC 9.4.2
public function get_order_count( &$customer ) { $count = apply_filters( 'woocommerce_customer_get_order_count', Users::get_site_user_meta( $customer->get_id(), 'wc_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 Users::update_site_user_meta( $customer->get_id(), 'wc_order_count', $count ); } return absint( $count ); }