WooCommerce: Adding data from the Checkout Field Editor

If you utilize WooCommerce along with the " Checkout field editor" to incorporate additional fields into your checkout form and aim to gather data to append to your Contact, here's what you need to do.

The pieces:

Insert the following code into your theme's functions.php file or a code snippets plugin.
<?php

/**
 * Map custom checkout fields to contact fields in Groundhogg
 * Groundhogg should have already created the initial contact record by this point
 * 
 * @param $order_id
 * @param $posted_data
 * @param $order \WC_Order
 */
function map_woocommerce_custom_fields_to_contact_record( $order_id, $posted_data, $order ) {

	$contact = \Groundhogg\get_contactdata( $order->get_billing_email() );

	if ( ! \Groundhogg\is_a_contact( $contact ) ) {
		return;
	}
	
	$contact->update_meta( 'my_custom_field', \Groundhogg\get_post_var( 'my_custom_checkout_field', 'Default Value' ) );
}
add_action( 'woocommerce_checkout_order_processed', 'map_woocommerce_custom_fields_to_contact_record', 11, 3 );

This will add your data to the Contacts as metadata under the "More" tab. You can then create Custom meta fields if desired. 

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us