WooCommerce

Use a Formidable form on your WooCommerce product pages. Create your form however you would like, add a calculation to generate the total, and select your form in the sidebar box on the WooCommerce page. The product price used in your WooCommerce cart comes from the last calculation field in your form.

Requires Formidable 2.0.11+

Download and install

  1. This add-on requires Formidable Forms Pro. If you haven't already done so, install and activate Formidable Forms Pro.
  2. Download the latest version of the WooCommerce plugin.
  3. In your WordPress admin, go to 'Plugins' > 'Add New' and click the 'Upload Plugin' button at the top of the page.
  4. Upload the zip file you just downloaded in step one. Once the plugin is installed, click 'Activate Plugin' or go to the 'Plugins' page, find 'Formidable WooCommerce' and click 'Activate'.

Setup

  1. Create a form. You'll need a calculation somewhere in your form to link up with the WooCommerce price.
  2. Create a WooCommerce product. The product should be the base price of the product. Set a price for your product, and then use your Formidable form to add to the base price. On the edit product page, add the form using the Choose a Form select box in the sidebar. Now save your product.
  3. Now your form will show when you view your product page, and adjust the prices of the items you add to your cart.

Customizations

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 and replace 25 in the code below with the ID of the hidden field. After a checkout is completed, the order ID will be added to the hidden field.

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 ) );
    $field_id = 25; // change 25 to the id of your order id field
    $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_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
// 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 );
}
}

Change field value after completed order

Use the following code to change a field value after a Woocommerce order is completed. Replace 25 with the ID of the form to update, replace 100 with the field ID to update, and replace 'Complete' with the new value you would like to use. Please note: You must have a default value submitted in the field in order for this function to work.

add_action( 'woocommerce_order_status_completed', 'update_frm_entry_after_wc_order_completed' );
function update_frm_entry_after_wc_order_completed( $order_id ) {
	$order = new WC_Order( $order_id );
	$items = $order->get_items();
	foreach ( $items as $item_id => $product ) {
		if ( isset( $product['formidable_form_data'] ) && is_numeric( $product['formidable_form_data'] ) ) {
			$entry_id = $product['formidable_form_data'];
			$entry = FrmEntry::getOne( $entry_id );

			if ( $entry && $entry->form_id == 25 ) {
				$field_id = 100;//Replace 100 with the field ID to update
				$new_value = 'Complete';
				FrmEntryMeta::update_entry_meta( $entry_id, $field_id, null, $new_value );
			}
		}
	}
}

Modify field types in order

Add specific field types to the Woocommerce order and cart. Replace textarea with the type of field that you would like to include in the cart.

add_filter( 'wc_fp_exclude_fields', 'frm_adjust_exclude_fields' );
function frm_adjust_exclude_fields( $exclude ) {
    $key = array_search( 'textarea', $exclude );
    if ( $key !== false ) {
        unset( $exclude[ $key ] );
    }
    return $exclude;
}

Remove specific fields from cart

Use this hook to remove specific fields from the Woocommerce order and cart. Replace 123, 345, and 456 with your fields IDs or keys.

add_filter( 'wc_fp_exclude_fields', 'frm_remove_fields_from_cart' );
function frm_remove_fields_from_cart( $exclude_fields ) {
    $exclude_field_ids = array( '123', '345', '456' );
    $exclude_fields = array_merge( $exclude_fields, $exclude_field_ids );
    return $exclude_fields;
}

Use a form field to update Woocommerce quantity

WooCommerce will accept input from your form as part of its cost calculation, though quantity is specified by a WooCommerce field. If you would like a form field to update the quantity as well, you can use this JavaScript.

<script>
jQuery(document).ready(function($){
    jQuery('#field_iw7oc').change(function(){
        var fpField = $("#field_iw7oc").val();
        var wooField = jQuery(".input-text.qty.text");
        if(fpField == ''){
            wooField.val("1");
        } else {
            wooField.val(fpField);
        }
    });
});
</script>

Change 'iw7oc' to your quantity field's key. This snippet can be placed in your form's "Settings" > "Customize HTML" > "After Fields" box.

Adjust values displayed in cart

This example will remove the ($2.00) string that is added to any numeric value in the cart. Replace 123, 124, and 125 with your field IDs.

add_filter( 'wc_fp_cart_item_data', 'modify_frm_woo_display', 10, 2 );
function modify_frm_woo_display( $values, $args ) {
    if ( in_array( $args['field']->id, array( 123, 124, 125 ) ) ) {
        $end_position = strpos( $values['display'], ' (' );
        $values['display'] = substr( $values['display'], 0, $end_position );
    }
    return $values;
}