Formidable Forms

Formidable Forms

  • Features
  • Pricing
  • Resources
    • Docs & Support
    • Blog
    • Community
  • Solutions
    • Web Applications
    • Calculators
    • Surveys
    • Directories
    • Payments
    • Contact forms
  • Login
  • Get Formidable Forms

woocommerce_new_order_item

Last Updated: June 1, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Add Ons → WooCommerce → woocommerce_new_order_item
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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 );
        }
    }
}
  • Usage
  • Parameters
  • Examples
    • Add Order ID to Entry
    • Dynamically add Order ID and save entry data with Order
    • Save the WooCommerce billing details to fields
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms

Using WordPress and want to get Formidable Forms for free?

Get Formidable Forms Lite Now

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Application Builder
  • Calculator Forms
  • Surveys & Polls
  • Quiz Maker
  • Form Templates
  • Application Templates
  • Directories
  • Donation Plugin

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • WP Tasty
  • Nutrifox

Copyright © 2023 Strategy11, LLC. Formidable FormsĀ® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews