The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Add Woocommerce Order Id and Save Entry Data With Order

Labels

This Discussion is public

Notifications

I recoded the Order ID example given by Formidable to allow use of multiple forms for different products.  I also saved the entry information with the order to allow for better usability in connection with an order.  Maybe it will be useful for someone else as well.

//Save WooCommerce Order ID To entry
add_action( 'woocommerce_add_order_item_meta', 'add_order_id_to_entry', 10, 2 );
function add_order_id_to_entry( $item_id, $cart_item ) {

// check if there's form data to process
if ( empty( $cart_item['_formidable_form_data'] ) ) {
return;
}

// get form entry
$entry = FrmEntry::getOne( $cart_item['_formidable_form_data'] );
if ( $entry ) {
global $wpdb;
$order_id  = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d ", $item_id ) );

//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
// You can comment out 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
// Also be sure to change '_custom_key' text to whatever you would like your keyname to be in the order meta
//Additional Person Name Field
$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 );
}
}

Thanks for sharing that!

Discussion closed.