#Overview
This filter is run during a WooCommerce checkout, after WP Fusion has extracted the customer data from the order object. It can be used to sync additional data from a WooCommerce order to custom fields in your CRM.
To use the code examples below, add them to your active theme’s functions.php file.
#Parameters
$customer_data
: This is an array of key value pairs representing WordPress meta fields and their corresponding values.$order
: The WooCommerce order object
#Examples
#Sync the WooCommerce total order value to a custom field
function get_order_total( $customer_data, $order ) {
$customer_data['order_total'] = $order->get_total();
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'get_order_total', 10, 2 );
#Sync the coupon code used on an order to a custom field
// Add the coupon field as available for sync on the Contact Field tab in the WPF settings
function wpf_coupon_meta_fields( $fields ) {
$fields['wc_coupon'] = array( 'label' => 'Coupon', 'type' => 'text', 'group' => 'woocommerce' );
return $fields;
}
add_filter( 'wpf_meta_fields', 'wpf_coupon_meta_fields' );
// Get the coupon code of the first coupon used on the order and add it to the order data
function wpf_sync_coupon( $customer_data, $order ) {
$coupons = $order->get_coupon_codes();
if( ! empty( $coupons ) ) {
$customer_data['wc_coupon'] = $coupons[0];
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wpf_sync_coupon', 10, 2 );
#Get a custom date field off an order product and sync it to the contact record
// Add the date field as available for sync on the Contact Field tab in the WPF settings
function wpf_product_meta_fields( $fields ) {
$fields['course_date'] = array( 'label' => 'Course Date', 'type' => 'date', 'group' => 'woocommerce' );
return $fields;
}
add_filter( 'wpf_meta_fields', 'wpf_product_meta_fields' );
// Get the date field off the product and merge it into the customer data
function get_product_data( $customer_data, $order ) {
foreach ( $order->get_items() as $item ) {
$product_id = $item->get_product_id();
$meta_value = get_post_meta( $product_id, 'course_date_field_key', true );
if ( ! empty( $meta_value ) ) {
$customer_data['course_date'] = $meta_value;
}
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 10, 2 );
#Sync the quantity of a specific order item to a custom field
// Add the field as available for sync on the Contact Field tab in the WPF settings
function wpf_product_meta_fields( $fields ) {
$fields['item_quantity'] = array( 'label' => 'Item Quantity', 'type' => 'int', 'group' => 'woocommerce' );
return $fields;
}
add_filter( 'wpf_meta_fields', 'wpf_product_meta_fields' );
// Get the quantity from the order item with product ID 123 and merge it into the customer data
function get_product_data( $customer_data, $order ) {
foreach ( $order->get_items() as $item ) {
if ( 123 === $item->get_product_id() ) {
$customer_data['item_quantity'] = $item->get_quantity();
}
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 10, 2 );
#Sync the customer’s lifetime value to a custom field
function wp_fusion_sync_lifetime_value_with_order( $customer_data, $order ) {
// Fetch orders with WooCommerce HPOS compatibility
$args = array(
'limit' => -1,
'customer' => $customer_data['billing_email'],
'status' => wc_get_is_paid_statuses(),
'orderby' => 'ID',
'order' => 'DESC',
);
// Query orders using wc_get_orders (compatible with HPOS)
$customer_orders = wc_get_orders( $args );
// Initialize lifetime value
$customer_data['lifetime_value'] = 0;
if ( ! empty( $customer_orders ) ) {
foreach ( $customer_orders as $customer_order ) {
// Get the total order amount
$order_total = $customer_order->get_total();
$customer_data['lifetime_value'] += floatval( $order_total );
}
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wp_fusion_sync_lifetime_value_with_order', 10, 2 );
#Ignore an order
You can return an empty value from the wpf_woocommerce_customer_data
filter in order to have WP Fusion ignore a WooCommerce order. In this example we’re going to ignore orders unless their status is completed
:
function wpf_only_allow_completed( $customer_data, $order ) {
if ( 'completed' !== $order->get_status() ) {
return null;
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wpf_only_allow_completed', 10, 2 );
#Don’t process orders in real time
In this example, we will not sync any orders in real time as customers check out on your store.
This can be used in combination with a scheduled batch operation to sync orders later, when there are more resources available on the site.
function wpf_dont_process_orders_in_real_time( $customer_data ) {
if ( did_action( 'woocommerce_checkout_process' ) ) {
return null;
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wpf_dont_process_orders_in_real_time' );
#Only sync existing customers
This example will only sync the order data to your CRM if the customer is a registered WordPress user and already has a CRM contact record. If it’s a new customer or existing user without a CRM contact ID, they will not be synced.
function only_sync_existing_contacts( $customer_data, $order ) {
if ( empty( wpf_get_contact_id( $order->get_user_id() ) ) ) {
return false;
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'only_sync_existing_contacts', 10, 2 );
#Only sync customers who opt in to marketing
Because WP Fusion uses tags applied at checkout to unlock content, enroll users into courses, and otherwise deliver access to purchased products— all customers will be synced with your CRM, regardless of whether or not they’ve consented to marketing.
It’s then up to you to use the tag and/or custom field value in your campaigns and automations to exclude contacts from marketing if they haven’t opted in.
On some simple stores, you may want to completely disable the sync of customers with your CRM if they haven’t opted in to marketing. This can be achieved with the following filter, added to your functions.php file:
function do_not_sync_unconfirmed_customers( $customer_data ) {
if ( empty( $customer_data['email_optin'] ) ) {
return false;
}
return $customer_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'do_not_sync_unconfirmed_customers' );