-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpf-woocommerce-lifetime-value.php
58 lines (38 loc) · 1.23 KB
/
wpf-woocommerce-lifetime-value.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
Plugin Name: WP Fusion - WooCommerce Lifetime Value
Description: Allows syncing a customer's lifetime value from WooCommerce to a custom field in your CRM
Plugin URI: https://wpfusion.com/
Version: 1.0
Author: Very Good Plugins
Author URI: https://verygoodplugins.com/
*/
function wpf_woo_ltv_field( $meta_fields ) {
$meta_fields['lifetime_value'] = array(
'label' => 'Lifetime Value',
'group' => 'woocommerce',
);
return $meta_fields;
}
add_filter( 'wpf_meta_fields', 'wpf_woo_ltv_field' );
function wpf_woo_ltv_calculate( $order_data, $order ) {
$order_data['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' => $order_data['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();
$order_data['lifetime_value'] += floatval( $order_total );
}
}
return $order_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wpf_woo_ltv_calculate', 10, 2 );