wpf_woocommerce_customer_data

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 customer’s lifetime value to a custom field

function example_sync_lifetime_value_with_order( $customer_data, $order ) {

	$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'     => $customer_data['billing_email'],
			'orderby'        => 'ID',
			'order'          => 'DESC',
		)
	);

	$customer_data['lifetime_value'] = 0;

	if ( ! empty( $customer_orders ) ) {

		foreach ( $customer_orders as $order_id ) {

			$order = wc_get_order( $order_id );

			$order_total = $order->get_total();

			$customer_data['lifetime_value'] += floatval( $order_total );

		}
	}

	return $customer_data;
}

add_filter( 'wpf_woocommerce_customer_data', 'example_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 );

Only sync existing customers

This example will only sync the order data to your CRM if the customer is logged in and already has a CRM contact record. If it’s a new customer 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 );

Was this helpful?