Created
March 17, 2021 08:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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