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
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
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
Copy link

kurtschlatzer commented May 22, 2019

Thank you!

@tonybahama
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
Copy link
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.

@JVDL-1
Copy link

JVDL-1 commented Feb 1, 2021

@bryceadams Thank you so much for this snippet. So much speed improvement for retrieving orders by our shipping partner. It would calculate total spend and order count for each order. Went from 100 orders in 5 minutes to about 30 sec!

Got a question, does the WooCommerce Analytics dashboard still works for total order count and total spent?
https://docs.woocommerce.com/document/woocommerce-analytics/#customers-report

Random picked some customers to check if the numbers still matched, which they seem to do. So that would indicate that WooCommerce Analytics dashboard does its own calculations. Is this right? Or did the snippet only disable the function partially?

@bryceadams
Copy link
Author

bryceadams commented Feb 1, 2021

@JVDL-1 great to hear! :) No idea how the WC Analytics dashboard works / calculates numbers so I'd recommend testing thoroughly on your end to be sure. If needed, you could probably adjust the code to only run when your shipping partner gets the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment