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
$order_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( $order_data, $order ) {
$order_data['order_total'] = $order->get_total();
return $order_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( $order_data, $order ) {
$coupons = $order->get_coupon_codes();
if( ! empty( $coupons ) ) {
$order_data['wc_coupon'] = $coupons[0];
}
return $order_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( $order_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 ) ) {
$order_data['course_date'] = $meta_value;
}
}
return $order_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 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( $order_data, $order ) {
if ( 'completed' !== $order->get_status() ) {
return null;
}
return $order_data;
}
add_filter( 'wpf_woocommerce_customer_data', 'wpf_only_allow_completed', 10, 2 );
Was this helpful?
Let us know if you liked the post. That’s the only way we can improve.