Skip to content

Instantly share code, notes, and snippets.

@ameeker
Created June 15, 2015 20:17
Show Gist options
  • Save ameeker/746dbe2c46cd4d3978c3 to your computer and use it in GitHub Desktop.
Save ameeker/746dbe2c46cd4d3978c3 to your computer and use it in GitHub Desktop.
Add WooCommerce Order ID column to Gravity Forms export
add_filter( 'gform_export_fields', 'add_fields', 10, 1 );
function add_fields( $form ) {
array_push( $form['fields'], array( 'id' => 'orderid', 'label' => __( 'Order ID', 'gravityforms' ) ) );
return $form;
}
add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $lead ) {
switch( $field_id ) {
case 'orderid' :
$value = 'echo $order->id;';
break;
}
return $value;
}
@robneu
Copy link

robneu commented Jun 15, 2015

I think something like this is what you're looking for. I'm not super familiar with WooCommerce though, so I could be wrong. I'm also not sure how you're trying to get the post ID. Pulling in the post global and checking the ID like that is going to give you the ID of the current post if this is being run within the loop. If it's outside the loop, it probably won't give you anything that you can use.

Where it this being run? What order are you trying to retrieve?

add_filter( 'gform_export_fields', 'add_fields', 10, 1 );
function add_fields( $form ) {
    array_push( $form['fields'],
        array(
            'id'    => 'orderid',
            'label' => __( 'Order ID', 'gravityforms' ),
        )
    );
    return $form;
}

add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $lead ) {
    global $woocommerce, $post;
    $order = new WC_Order( $post->ID );
    if ( 'orderid' === $field_id && is_object( $order ) ) {
        $value = $order->id;
    }
    return $value;
}

Also, if you're doing this within the loop you really don't need either of those globals. You can use get_the_ID() and the WooCommerce global isn't really in use at all in the current code.

@ameeker
Copy link
Author

ameeker commented Jun 15, 2015

Hi Rob,
It's pulling from a Gravity Forms export. We are using the Gravity Forms product add on for Woo. The gravity forms export of the form includes all of the order information we need in a form we can use it, but it doesn't incclude the order ID

@robneu
Copy link

robneu commented Jun 15, 2015

Well, depending on how that extension works you may be able to get the order number by using the $form_id. Something like this:

add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $lead ) {
    $order = new WC_Order( $form_id );
    if ( 'orderid' === $field_id && is_object( $order ) ) {
        $value = $order->id;
    }
    return $value;
}

If that doesn't work, if the form is submitted on the product's page you can retrieve that from the $lead parameter like this:

add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $lead ) {
    $order = new WC_Order( url_to_postid( $lead['source_url'] ) );
    if ( 'orderid' === $field_id && is_object( $order ) ) {
        $value = $order->id;
    }
    return $value;
}

I think that's about the best I can do with the given information and my understanding of the plugins you're using. It might work... or not... 😀

@ameeker
Copy link
Author

ameeker commented Jun 16, 2015

The first example returns the form id; the second returns a 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment