Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculates a WooCommerce lifetime value for registered users when syncing metadata to the CRM
<?php
function example_sync_lifetime_value( $user_meta, $user_id ) {
$user_meta['lifetime_value'] = 0;
$customer_orders = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => wc_get_is_paid_statuses(),
'meta_key' => '_billing_email',
'meta_value' => $user_meta['billing_email'],
'orderby' => 'ID',
'order' => 'DESC',
)
);
if ( ! empty( $customer_orders ) ) {
foreach ( $customer_orders as $order_id ) {
$order = wc_get_order( $order_id );
$order_total = $order->get_total();
$user_meta['lifetime_value'] += floatval( $order_total );
}
}
return $user_meta;
}
add_filter( 'wpf_get_user_meta', 'example_sync_lifetime_value', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment