From 2ea61e9a863ab577a2b7012f7d6a8215263c3fea Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Thu, 20 Dec 2018 11:11:24 -0700 Subject: [PATCH] Move registered customer lookup update logic into data store class. --- .../includes/class-wc-admin-api-init.php | 48 ++--------------- ...-wc-admin-reports-customers-data-store.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php index e560023621d..99e5c787d0a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php @@ -363,51 +363,13 @@ class WC_Admin_Api_Init { // Process customers until close to running out of memory timeouts on large sites then requeue. foreach ( $customer_ids as $customer_id ) { - $customer = new WC_Customer( $customer_id ); - $order_count = $customer->get_order_count( 'edit' ); - $total_spend = $customer->get_total_spent( 'edit' ); - $last_order = $customer->get_last_order(); - $last_active = $customer->get_meta( 'wc_last_active', true, 'edit' ); + $result = WC_Admin_Reports_Customers_Data_Store::update_registered_customer( $customer_id ); - // TODO: handle existing row in lookup table. - $wpdb->replace( - $wpdb->prefix . 'wc_customer_lookup', - array( - 'user_id' => $customer_id, - 'username' => $customer->get_username( 'edit' ), - 'first_name' => $customer->get_first_name( 'edit' ), - 'last_name' => $customer->get_last_name( 'edit' ), - 'email' => $customer->get_email( 'edit' ), - 'city' => $customer->get_billing_city( 'edit' ), - 'postcode' => $customer->get_billing_postcode( 'edit' ), - 'country' => $customer->get_billing_country( 'edit' ), - 'orders_count' => $order_count, - 'total_spend' => $total_spend, - 'avg_order_value' => $order_count ? ( $total_spend / $order_count ) : 0, - 'date_last_order' => $last_order ? date( 'Y-m-d H:i:s', $last_order->get_date_created( 'edit' )->getTimestamp() ) : '', - 'date_registered' => date( 'Y-m-d H:i:s', $customer->get_date_created( 'edit' )->getTimestamp() ), - 'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : '', - ), - array( - '%d', - '%s', - '%s', - '%s', - '%s', - '%s', - '%s', - '%s', - '%d', - '%f', - '%f', - '%s', - '%s', - '%s', - ) - ); + if ( $result ) { + // Pop the customer ID from the array for updating the transient later should we near memory exhaustion. + unset( $customer_ids[ $customer_id ] ); + } - // Pop the customer ID from the array for updating the transient later should we near memory exhaustion. - unset( $customer_ids[ $customer_id ] ); if ( $updater instanceof WC_Background_Updater && $updater->is_memory_exceeded() ) { // Update the transient for the next run to avoid processing the same orders again. set_transient( 'wc_update_350_all_customers', $customer_ids, DAY_IN_SECONDS ); diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php index 2649adaca6a..0142ed2a1f1 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-customers-data-store.php @@ -299,6 +299,59 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store return $data; } + /** + * Update the database with customer data. + * + * @param int $user_id WP User ID to update customer data for. + * @return int|bool|null Number or rows modified or false on failure. + */ + public static function update_registered_customer( $user_id ) { + global $wpdb; + + $customer = new WC_Customer( $user_id ); + $order_count = $customer->get_order_count( 'edit' ); + $total_spend = $customer->get_total_spent( 'edit' ); + $last_order = $customer->get_last_order(); + $last_active = $customer->get_meta( 'wc_last_active', true, 'edit' ); + + // TODO: try to preserve customer_id for existing user_id? + return $wpdb->replace( + $wpdb->prefix . self::TABLE_NAME, + array( + 'user_id' => $user_id, + 'username' => $customer->get_username( 'edit' ), + 'first_name' => $customer->get_first_name( 'edit' ), + 'last_name' => $customer->get_last_name( 'edit' ), + 'email' => $customer->get_email( 'edit' ), + 'city' => $customer->get_billing_city( 'edit' ), + 'postcode' => $customer->get_billing_postcode( 'edit' ), + 'country' => $customer->get_billing_country( 'edit' ), + 'orders_count' => $order_count, + 'total_spend' => $total_spend, + 'avg_order_value' => $order_count ? ( $total_spend / $order_count ) : 0, + 'date_last_order' => $last_order ? date( 'Y-m-d H:i:s', $last_order->get_date_created( 'edit' )->getTimestamp() ) : '', + 'date_registered' => date( 'Y-m-d H:i:s', $customer->get_date_created( 'edit' )->getTimestamp() ), + 'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : '', + ), + array( + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%d', + '%f', + '%f', + '%s', + '%s', + '%s', + ) + ); + } + /** * Returns string to be used as cache key for the data. *