<?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; | |
} |
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;
}
Thank you!
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?
@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.
@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?
@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.
I popped this into a plugin and here's the result in New Relic:
Can't thank you enough.