Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable WooCommerce total spent / order count meta calculations
<?php
add_filter( 'get_user_metadata', 'mtkdocs_filter_user_metadata', 10, 4 );
function mtkdocs_filter_user_metadata( $value, $object_id, $meta_key, $single ) {
// Check if it's one of the keys we want to filter
if ( in_array( $meta_key, array( '_money_spent', '_order_count' ) ) ) {
// Return 0 so WC doesn't try calculate it
return 0;
}
// Default
return $value;
}
@kurtschlatzer

This comment has been minimized.

Copy link

kurtschlatzer commented May 21, 2019

I popped this into a plugin and here's the result in New Relic:

Screen Shot 2019-05-21 at 10 20 06 AM

Can't thank you enough.

@lkraav

This comment has been minimized.

Copy link

lkraav commented May 21, 2019

It's also possible to keep already calculated values alive

add_filter( 'get_user_metadata', 'mtkdocs_filter_user_metadata', 10, 4 );
function mtkdocs_filter_user_metadata( $value, $object_id, $meta_key, $single ) {
    // Check if it's one of the keys we want to filter
    if ( in_array( $meta_key, array( '_money_spent', '_order_count' ) ) ) {
        if ( '' === $value ) {
            // Return 0 so WC doesn't try calculate it
            $value = 0;
        }
    }

    return $value;
}
@kurtschlatzer

This comment has been minimized.

Copy link

kurtschlatzer commented May 22, 2019

Thank you!

@tonybahama

This comment has been minimized.

Copy link

tonybahama commented Dec 1, 2019

Hi!
Does this have side effects if I use this?

If I checked correctly, this function runs when an order is put in completed status and every time we do a "completed" status it checks all orders to update customer data?

@bryceadams

This comment has been minimized.

Copy link
Owner Author

bryceadams commented Feb 21, 2020

@kurtschlatzer my absolute pleasure!

@tonybahama the only side effect is that it stops the 'total spent' and 'order count' data from being stored in each customer's data, but this is very rarely needed directly within Woo so you can probably go without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.