Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active June 15, 2020 02:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webaware/6f0fd469855e7c2d5b8495b9708c8003 to your computer and use it in GitHub Desktop.
Save webaware/6f0fd469855e7c2d5b8495b9708c8003 to your computer and use it in GitHub Desktop.
specify a custom Invoice Reference for Gravity Forms eWAY (Free and Pro) transactions. https://gfeway.webaware.net.au/
<?php
/*
Plugin Name: GFeWAY Invoice Reference
Plugin URI: https://gist.github.com/webaware/6f0fd469855e7c2d5b8495b9708c8003
Description: specify Invoice Reference for Gravity Forms eWAY transaction
Version: 1
Author: WebAware
Author URI: https://gfeway.webaware.net.au/
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* change the Invoice Reference field to a custom value
* @param string $invoiceReference
* @param array $form the Gravity Forms form
* @return string
*/
add_filter('gfeway_invoice_ref', function($invoiceReference, $form) {
// set your custom reference here; perhaps a WordPress User ID, or field value
$invoiceReference = uniqid();
return $invoiceReference;
}, 10, 2);
@samheavyside
Copy link

Hi.

This looks simple enough, but I am wondering how I would use the data from the submitted form as the invoice reference (I'm using the free version of the GFEway plugin).

Example:

If I have a simple Text field - Invoice Ref: ________

How can I use the value entered as the InvoiceReference?

This seems like it should be doable, but it seems just the $form object itself is included in the filter, not the entry data? Is that correct?

@webaware
Copy link
Author

webaware commented Feb 4, 2020

@samheavyside:

This seems like it should be doable, but it seems just the $form object itself is included in the filter, not the entry data? Is that correct?

Yes, in the free add-on (and the pro add-on with Direct mode), the transaction happens before the entry is created. You can catch the form post to get the field value though. Look in $_POST (e.g. with xdebug, or log to the WP debug.log) to see what to pluck from that array.

cheers,
Ross

@cjwebdev
Copy link

Hey @samheavyside,

Did you get this working in the end?

Fancy sharing? ;)

CJ

@samheavyside
Copy link

Hi @cjwebdev,

Yes, I got it working.

1. I added a 'single-line text' field to the Form so the user could specify a Reference Number.

Note: The field had an ID of 5 - see attached image:

invoice reference field

Your field will likely have a different ID. Keep that in mind as you will be using the ID in the filter.

2. I added the following to the functions.php:

add_filter('gfeway_invoice_ref', function($invoiceReference, $form) {
  
   //Check that we are applying the filter for the correct form (form ID 3) using and that the correct field (field ID 5) is set.
   if( $form['id'] == 3 && isset($_POST['input_5']) ){

        // If it is, set the $invoiceReference to the value of the Field.
        $invoiceReference = $_POST['input_5'];
    }
 
    return $invoiceReference;

}, 10, 2);

@cjwebdev
Copy link

Thanks @samheavyside,

That's great you can access the $_POST object so easily.

Really appreciate you sharing - have a great day!

CJ

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