Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Created March 17, 2021 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/33f01c71999247875cf72c76fe3ba60b to your computer and use it in GitHub Desktop.
Save verygoodplugins/33f01c71999247875cf72c76fe3ba60b to your computer and use it in GitHub Desktop.
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