In this tutorial I’ll explain how to retrieve Woocommerce order data and use it to submit a gravity form entry.

Let’s get started…

Step1: Create a function and hook it to woocommerce

The first will be creating a function that is triggered after a new order, for this tutorial we will be using the action hook ( woocommerce_thankyou ); which is triggered after a successful checkout, the starting code would look something like this:

function action_woocommerce_thankyou( $order_id ) {
// content here
};

add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );  

Step2: Retrieve Order Data

Now that our function is ready, we need to retrieve order data, which can be done using the class ( WC_Order ):

$order = new WC_Order( $order_id );

The variable above will return an array of data, which we will use to extract the specific data we need submitted to a gravity form later, you can get an idea of what data is associated with a new order and can be retrieved from Woocommerce Wiki on Github.

For this tutorial we will work only with line item (product) data. So from order array, we will grab line item array like this:

$line_items = $order->get_items();

Now that we have access to product details, we need to create a variable for each piece of data we want to add:

$product_id = $line_item['product_id'];
$product_name = $line_item['name'];
$meta = $line_item['meta'];
$qty = $line_item['qty'];
$price = $line_item['line_total'];

Now that we have product details,  let’s combine these together:

function action_woocommerce_thankyou( $order_id ) {
$order = new WC_Order( $order_id );

 $line_items = $order->get_items();

$product_id = $line_item['product_id'];
$product_name = $line_item['name'];
$meta = $line_item['meta'];
$qty = $line_item['qty'];
$price = $line_item['line_total'];
};

add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );  

Step3: Populate and submit a gravity form

Last step is submitting a gravity form programmatically using same data the we retrieved earlier, to do this we need to define Gravity form ID, and fields first:

$form_id = 10;
$entry = array(
"form_id" => $form_id,
"1" => $order_id,
"2" => $product_id,
"3" => $product_name,
"4" => $qty,
"5" => $price,
);

After we specified form ID and field IDs each with its appropriate value, we can now create an entry with the function ( add_entry ); make sure to specify the array of entry objects we created earlier in this function:

$create_entry = GFAPI::add_entry($entry);

The final code would look like this:

function action_woocommerce_thankyou( $order_id ) {
$order = new WC_Order( $order_id );

$line_items = $order->get_items();

$product_id = $line_item['product_id'];
$product_name = $line_item['name'];
$meta = $line_item['meta'];
$qty = $line_item['qty'];
$price = $line_item['line_total'];

$form_id = 10;
$entry = array(
"form_id" => $form_id,
"2" => $order_id,
"3" => $product_id,
"7" => $product_name,
"6" => $qty,
"6" => $price,
);
$create_entry = GFAPI::add_entry($entry);
};
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 

Conclusion

In some cases like order import or creating orders from WordPress dashboard, the function above will not work, so you’ll need to use an action hook that works for your needs Eg:(save_post). WordPress hooks will not return Order_id, as a workaround you can assign post_id instead to get the data that you need.