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:
- Groundhogg Core and the Pro plan
- WooCommerce Integration Plugin
- WooCommerce Plugin
- Checkout Field Editor
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.