Dynelight
Dynelight

Reputation: 2162

Link between Gravity Forms and Woocommerce APIs

I have a Woocommerce site, and I use Gravity Forms to further expand each order.

I am coding a management tool that consumes both APIs to make some statistics and other administration tools.

I can get a list of the Gravity Forms entries, and also a list of the orders. The problem I have is that I don't know how can I get the entry that is related to a particular order.

Is there a way to do this?

Upvotes: 2

Views: 1706

Answers (4)

MJJ
MJJ

Reputation: 109

I was able to do this using Gravity Forms, Gravity Forms Product Addons, and Woocommerce using a three step process:

STEP 1: Get GF Entry ID/s as the entry is made and store it in a session. This happens before checkout is complete and sometimes before the user is logged in.

add_action( 'gform_after_submission', 'blb_get_lead_entry_id', 10, 2 );
function blb_get_lead_entry_id( $entry, $form ) {

    $meta_value = $entry['id'];

    //session array with all the entries because GF creates 2 entries each time for validation purposes apparently
    if (!isset($_SESSION['entryclump'])) {
    $_SESSION['entryclump'] = array();
    }
    $_SESSION['entryclump'][]  = $meta_value;

    //also an array with just the current entry which will end up be the later of the two entries added by GF
    $_SESSION[ 'gf-entry-id' ] = $meta_value;
}

STEP 2: Include the entry you just gathered ($_SESSION[ 'gf-entry-id' ]) with the Cart Item Meta and then save it.

In this case, i am saving a field called "_gf_entry_ID" which will get added to the woocommerce_order_itemmeta table with the correct order_item_id and the later of the two GF entries as the meta_value.

//add cart item data
add_filter( 'woocommerce_add_cart_item_data', 'blb_add_gfentry_to_cart_data', 10, 3 );
function blb_add_gfentry_to_cart_data( $cartItemData, $productId, $variationId ) {
    $entryid=$_SESSION[ 'gf-entry-id' ];
    $cartItemData['GFentryID'] = $entryid;
    return $cartItemData;
    unset($_SESSION[ 'gf-entry-id' ]);
}


//add cart item data: session stuff
add_filter( 'woocommerce_get_cart_item_from_session', 'blb_cart_item_session', 10, 3 );
function blb_cart_item_session( $cartItemData, $cartItemSessionData, $cartItemKey ) {
    if ( isset( $cartItemSessionData['GFentryID'] ) ) {
        $cartItemData['GFentryID'] = $cartItemSessionData['GFentryID'];
    }

    return $cartItemData;
}

//save the data
add_action( 'woocommerce_add_order_item_meta', 'blb_save_gfentry', 10, 3 );
function blb_save_gfentry( $itemId, $values, $key ) {
    if ( isset( $values['GFentryID'] ) ) {
    wc_add_order_item_meta( $itemId, '_gf_entry_ID', $values['GFentryID'] );
    }
}

STEP 3 (optional): Update the GF Form to reflect the correct created_by user ID. Now that checkout is complete and the user is logged in, we can do that.

function blb_woocommerce_thankyou( $order_id ) { 
    //Current User
    $currentUserID = wp_get_current_user()->ID;

    //GF Entry Array for Order
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    $order_item_ids = array();
    $gf_entry_ids = array();
    foreach ( $items as $key=>$item ) {
      $gf_entry_ids[] = $item['item_meta']['_gf_entry_ID'][0];   
    }

    //First real quick clear all the entries in the entry clump (in case the user was already logged in)
    //This is important because GF creates two forms for product add ons with Woocommerce and we only want one to show up in the list and edit plugin
    $entryclump = $_SESSION[ 'entryclump' ];
    foreach ( $entryclump as $entry ) {
    global $wpdb;
    $wpdb->update('wp_rg_lead', array('created_by' => null), array('id' => $entry));
    }


    //Update wp_rg_lead
    if (($currentUserID!=0) && (isset($_SESSION[ 'entryclump' ])) ) {
        foreach ( $gf_entry_ids as $gf_entry_id ) {
        global $wpdb;
        $wpdb->update('wp_rg_lead', array('created_by' => $currentUserID), array('id' => $gf_entry_id));
        } //foreach
    } //if

    unset($_SESSION[ 'entryclump' ]);


}; 
add_action( 'woocommerce_thankyou', 'blb_woocommerce_thankyou', 10, 1 ); 

Upvotes: 0

rex a.k.
rex a.k.

Reputation: 1

This is where I found the link between WooCommerce and the gravity forms product addon:

In the database, find the order in the table your_table_prefix_posts, and grab its ID. I was filtering for the post_type "shop_order."

In the table your_table_prefix_woocommerce_order_items, find the ID just found and filter for "line_item" in the "order_item_type" column, and grab the "order_item_id."

Use that "order_item_id" to find the order's meta in the table your_table_prefix_woocommerce_order_itemmeta.

All of the order's gravity forms data is in there. It looks like there is no actual tie between what woo does and gravity, except that the form is filled out and it data is grabbed and stuck into your_table_prefix_woocommerce_order_itemmeta. I cannot find anything that ties a particular order to a particular gf entry, but you can get the data from Woo, and at least use that to search GF entries.

Upvotes: 0

Hertzel Armengol
Hertzel Armengol

Reputation: 64

have you tried with the woocomerce history plugin or fetching the raw metadata out the item¿?

wc_get_order_item_meta($order_item_id, "_gravity_forms_history"); wc_get_order_item_meta($order_item_id, "_gravity_form_data");

keep in mind that this will require a new endpoint to be created is not put of the box.

Upvotes: 1

Dave from Gravity Wiz
Dave from Gravity Wiz

Reputation: 2859

The last time I worked with the WooCommerce Gravity Forms Product Addons (a year or so ago) it did not store the order ID in the entry (would have to happen after the entry is created and after the order is created), or the entry ID in the order. The latter probably makes more sense but both would require custom code.

Again, it's been some time since I worked with the add-on. I'd ping WC support and see if they any tips on implementing support for this.

Upvotes: 0

Related Questions