This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook adds the Woocommerce order ID to a field in a Formidable form after the order is placed in Woocommerce.
Usage
add_action( 'woocommerce_new_order_item', 'add_order_woocommerce', 10, 3 );
Parameters
- None
Examples
Add Order ID to Entry
Use the following code to add the Woocommerce order ID to a field in a Formidable form after the order is placed in Woocommerce. You will need to add a hidden field to your form. After a checkout is completed, the order ID will be added to the hidden field. Replace 25 with the ID of your form and replace 123 with the ID of the hidden field.
add_action( 'woocommerce_new_order_item', 'add_order_id_to_entry', 10, 3 );
function add_order_id_to_entry( $item_id, $cart_item, $order_id ) {
// check if there's form data to process
if ( empty( $cart_item->legacy_values['_formidable_form_data'] ) ) {
return;
}
// get form entry
$entry = FrmEntry::getOne( $cart_item->legacy_values['_formidable_form_data'] );
if ( $entry && $entry->form_id == 25 ) {
$field_id = 123;
$added = FrmEntryMeta::add_entry_meta( $entry->id, $field_id, null, $order_id );
if ( ! $added ) {
FrmEntryMeta::update_entry_meta( $entry->id, $field_id, null, $order_id );
}
}
}
Dynamically add Order ID and save entry data with Order
An extension of the 'Add Order ID to Entry' customization
add_action( 'woocommerce_new_order_item', 'save_entry_data_with_order', 10, 3 );
function save_entry_data_with_order( $item_id, $cart_item, $order_id ) {
// check if there's form data to process
if ( empty( $cart_item->legacy_values['_formidable_form_data'] ) ) {
return;
}
// get form entry
$entry = FrmEntry::getOne( $cart_item->legacy_values['_formidable_form_data'] );
if ( $entry ) {
global $wpdb;
// Order ID Field - Change 'woo order id' text to name of order id field in your form
$field_id = $wpdb->get_var ( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_fields where name = 'woo order id' and form_id = %d ", $entry->form_id) );
$added = FrmEntryMeta::add_entry_meta( $entry->id, $field_id, null, $order_id );
if ( ! $added ) {
FrmEntryMeta::update_entry_meta( $entry->id, $field_id, null, $order_id );
}
// code below here is to save any custom fields to the WooCommerce Order Meta table to allow for easier use when combining with orders
// note that you can comment out any or all below if you do not want to save entry data with the order meta
// you will need to change the Name of the field in each query to the name of your field you want saved to the order
// be sure to change '_custom_key' text to whatever you would like your keyname to be in the order meta
$field_id = $wpdb->get_var ( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_fields where name = 'Additional Person Name' and form_id = %d ", $entry->form_id) );
$meta_value = FrmEntryMeta::get_meta_value( $entry, $field_id );
if($meta_value === NULL) {$meta_value='';}
update_metadata( 'post', $order_id, '_custom_key_1', $meta_value );
// Additional Person Email Field
$field_id = $wpdb->get_var ( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_fields where name = 'Additional Person Email' and form_id = %d ", $entry->form_id) );
$meta_value = FrmEntryMeta::get_meta_value( $entry, $field_id );
if($meta_value === NULL) {$meta_value='';}
update_metadata( 'post', $order_id, '_custom_key_2', $meta_value );
// Additional Person Phone Number Field
$field_id = $wpdb->get_var ( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_fields where name = 'Additional Person Phone Number' and form_id = %d ", $entry->form_id) );
$meta_value = FrmEntryMeta::get_meta_value( $entry, $field_id );
if($meta_value === NULL) {$meta_value='';}
update_metadata( 'post', $order_id, '_custom_key_3', $meta_value );
// Total Field
$field_id = $wpdb->get_var ( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_fields where name = 'Total' and form_id = %d ", $entry->form_id) );
$meta_value = FrmEntryMeta::get_meta_value( $entry, $field_id );
if($meta_value === NULL) {$meta_value='';}
update_metadata( 'post', $order_id, '_custom_key_4', $meta_value );
}
}
Save the WooCommerce billing details to fields
This example is meant to save the WooCommerce billing details to fields in a Formidable form after the order is placed.
add_action( 'woocommerce_new_order_item', 'add_order_id_to_entry', 10, 3 );
function add_order_id_to_entry( $item_id, $cart_item, $order_id ) {
// check if there's form data to process
if ( empty( $cart_item->legacy_values['_formidable_form_data'] ) ) {
return;
}
// get form entry
$entry = FrmEntry::getOne( $cart_item->legacy_values['_formidable_form_data'] );
if ( $entry && $entry->form_id == 110 ) { //change 110 to the Form ID.
$order = new WC_Order( $order_id );
$useremail = 2122; //change 2122 to the ID of the email field in the form
$userphone = 2126; //change 2126 to ID of the phone field in the form
$billingemail = $order->get_billing_email();
$billingphone = $order->get_billing_phone();
$added = FrmEntryMeta::add_entry_meta( $entry->id, $useremail, null, $billingemail );
$phone= FrmEntryMeta::add_entry_meta( $entry->id, $userphone, null, $billingphone );
if ( ! $added && !$phone ) {
FrmEntryMeta::update_entry_meta( $entry->id, $useremail, null, $billingemail );
FrmEntryMeta::update_entry_meta( $entry->id, $userphone, null, $billingphone );
}
}
}