Added cache and filters for wc-customer get operations.

This commit is contained in:
Julià Mestieri 2020-08-17 10:57:09 +02:00
parent 5c4d42b9de
commit b2c35a5ded
5 changed files with 33 additions and 16 deletions

View File

@ -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 '<div class="updated"><p>' . sprintf( esc_html__( 'Refreshed stats for %s', 'woocommerce' ), esc_html( $user->display_name ) ) . '</p></div>';
}

View File

@ -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.

View File

@ -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;

View File

@ -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;
}

View File

@ -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() ) {