Skip to content

Commit

Permalink
Use meta as a cache for order/spent methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Oct 14, 2016
1 parent ea1f9ae commit cfbb8d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 66 deletions.
60 changes: 35 additions & 25 deletions includes/class-wc-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,21 @@ public function get_last_order() {
* @return integer
*/
public function get_order_count() {
global $wpdb;

$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 ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
AND meta_value = '" . esc_sql( $this->get_id() ) . "'
" );
$count = get_user_meta( $this->get_id(), '_order_count', true );

if ( '' === $count ) {
global $wpdb;

$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 ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
AND meta_value = '" . esc_sql( $this->get_id() ) . "'
" );
update_user_meta( $this->get_id(), '_order_count', $count );
}

return absint( $count );
}
Expand All @@ -275,21 +280,26 @@ public function get_order_count() {
* @return float
*/
public function get_total_spent() {
global $wpdb;

$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
AND meta2.meta_key = '_order_total'
" );

if ( ! $spent ) {
$spent = 0;
$spent = get_user_meta( $this->get_id(), '_money_spent', true );

if ( '' === $spent ) {
global $wpdb;

$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
AND meta2.meta_key = '_order_total'
" );

if ( ! $spent ) {
$spent = 0;
}
update_user_meta( $this->get_id(), '_money_spent', $spent );
}

return wc_format_decimal( $spent, 2 );
Expand Down
44 changes: 4 additions & 40 deletions includes/wc-user-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,27 +477,8 @@ function wc_get_customer_available_downloads( $customer_id ) {
* @return string
*/
function wc_get_customer_total_spent( $user_id ) {
$spent = get_user_meta( $user_id, '_money_spent', true );
if ( '' === $spent ) {
global $wpdb;

$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = $user_id
AND posts.post_type IN ('" . implode( "','", wc_get_order_types( 'reports' ) ) . "')
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
AND meta2.meta_key = '_order_total'
" );

update_user_meta( $user_id, '_money_spent', $spent );
}

return $spent;
$customer = new WC_Customer( $user_id );
return $customer->get_total_spent();
}

/**
Expand All @@ -506,25 +487,8 @@ function wc_get_customer_total_spent( $user_id ) {
* @return int
*/
function wc_get_customer_order_count( $user_id ) {
$count = get_user_meta( $user_id, '_order_count', true );
if ( '' === $count ) {
global $wpdb;

$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 IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
AND posts.post_status IN ('" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "')
AND meta_value = $user_id
" );

update_user_meta( $user_id, '_order_count', absint( $count ) );
}

return absint( $count );
$customer = new WC_Customer( $user_id );
return $customer->get_order_count();
}

/**
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woocommerce/wo
* Improved the zone setup flow.
* Made wc_get_wildcard_postcodes return the orignal postcode plus * since wildcards should match empty strings too.
* New gallery on single product pages with better mobile support, PhotoSwipe and Zoom.
* Removed last order from customers part of the API due to performance concerns - use orders endpoint instead. Other order data on the endpoint is now transient cached.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce/master/CHANGELOG.txt).

Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/api/customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function test_customer_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertEquals( 16, count( $properties ) );
$this->assertEquals( 15, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'date_created', $properties );
$this->assertArrayHasKey( 'date_modified', $properties );
Expand Down

0 comments on commit cfbb8d6

Please sign in to comment.