From b2c35a5ded12b7b9a2bb7f6ab866cc90e07c4ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A0=20Mestieri?= Date: Mon, 17 Aug 2020 10:57:09 +0200 Subject: [PATCH] Added cache and filters for wc-customer get operations. --- .../reports/class-wc-report-customer-list.php | 1 + includes/class-wc-post-data.php | 3 +- .../class-wc-customer-data-store.php | 43 ++++++++++++------- includes/wc-order-functions.php | 1 + includes/wc-update-functions.php | 1 + 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/includes/admin/reports/class-wc-report-customer-list.php b/includes/admin/reports/class-wc-report-customer-list.php index e223570b7b9..4beeb951df9 100644 --- a/includes/admin/reports/class-wc-report-customer-list.php +++ b/includes/admin/reports/class-wc-report-customer-list.php @@ -62,6 +62,7 @@ class WC_Report_Customer_List extends WP_List_Table { delete_user_meta( $user_id, '_money_spent' ); delete_user_meta( $user_id, '_order_count' ); + delete_user_meta( $user_id, '_last_order' ); /* translators: User display name */ echo '

' . sprintf( esc_html__( 'Refreshed stats for %s', 'woocommerce' ), esc_html( $user->display_name ) ) . '

'; } diff --git a/includes/class-wc-post-data.php b/includes/class-wc-post-data.php index 39b89ba29b9..e09d8597190 100644 --- a/includes/class-wc-post-data.php +++ b/includes/class-wc-post-data.php @@ -406,8 +406,9 @@ class WC_Post_Data { $customer->save(); } - // Delete order count meta. + // Delete order count and last order meta. delete_user_meta( $customer_id, '_order_count' ); + delete_user_meta( $customer_id, '_last_order' ); } // Clean up items. diff --git a/includes/data-stores/class-wc-customer-data-store.php b/includes/data-stores/class-wc-customer-data-store.php index a772e45fe76..81137e995da 100644 --- a/includes/data-stores/class-wc-customer-data-store.php +++ b/includes/data-stores/class-wc-customer-data-store.php @@ -324,22 +324,31 @@ class WC_Customer_Data_Store extends WC_Data_Store_WP implements WC_Customer_Dat * @return WC_Order|false */ 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 + $last_order = apply_filters( + 'woocommerce_customer_get_last_order', + get_user_meta( $customer->get_id(), '_last_order', true ), + $customer ); - if ( ! $last_order ) { + if ( '' === $last_order ) { + 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 + ); + update_user_meta( $customer->get_id(), '_last_order', $last_order ); + } + + if ( ! $last_order || '' === $last_order ) { return false; } @@ -354,7 +363,11 @@ class WC_Customer_Data_Store extends WC_Data_Store_WP implements WC_Customer_Dat * @return integer */ public function get_order_count( &$customer ) { - $count = get_user_meta( $customer->get_id(), '_order_count', true ); + $count = apply_filters( + 'woocommerce_customer_get_order_count', + get_user_meta( $customer->get_id(), '_order_count', true ), + $customer + ); if ( '' === $count ) { global $wpdb; diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index 99f55dd30f2..ffe993acb83 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -461,6 +461,7 @@ function wc_delete_shop_order_transients( $order = 0 ) { $order_id = $order->get_id(); delete_user_meta( $order->get_customer_id(), '_money_spent' ); delete_user_meta( $order->get_customer_id(), '_order_count' ); + delete_user_meta( $order->get_customer_id(), '_last_order' ); } else { $order_id = 0; } diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index 2ebb79abdac..264ea4fd020 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -701,6 +701,7 @@ function wc_update_230_options() { // _money_spent and _order_count may be out of sync - clear them delete_metadata( 'user', 0, '_money_spent', '', true ); delete_metadata( 'user', 0, '_order_count', '', true ); + delete_metadata( 'user', 0, '_last_order', '', true ); // To prevent taxes being hidden when using a default 'no address' in a store with tax inc prices, set the woocommerce_default_customer_address to use the store base address by default. if ( '' === get_option( 'woocommerce_default_customer_address', false ) && wc_prices_include_tax() ) {