Easy Digital Downloads is an awesome free plugin for WordPress that allows you to transform your site to an e-commerce platform to sell your digital products. For most shops that sell digital products with Easy Digital Downloads, the default checkout form is plenty to get a customer’s order processed. However, there may be times when you require additional fields on the checkout page. This tutorial will show you how to easily add a custom field to Easy Digital Downloads checkout page.
Note: This tutorial assumes that you have some knowledge of functions and filters. If you do not, it is not advisable to continue on your own.
Custom code shown below should be copied into your child theme’s functions.php file, or a site specific plugin.
You can easily add custom fields to Easy Digital Downloads Checkout with Conditional Checkout Fields.
Add a Custom Checkout Field to Easy Digital Downloads
The first thing we need to do is add a field to the checkout page. Now, fields can come in many different varieties, such as text boxes, radio buttons, checkboxes, select menus, etc. For purposes of this example, I’m just going to show a simple text box, but it shouldn’t be difficult to swap out the type of field.
<?php add_action( 'edd_purchase_form_user_info_fields' , 'sd_edd_checkout_field' ); function sd_edd_checkout_field(){ ?> <label class="edd-label" for="sd-edd-checkout-field"> <?php _e( 'My Field Title' ); ?><span class="edd-required-indicator">*</span> </label> <span class="edd-description"> <?php _e( 'Optional Field Label' ); ?> </span> <input class="edd-input" type="text" name="sd_edd_checkout_field" id="sd-edd-checkout-field" placeholder="<?php _e( 'Optional placeholder text' ); ?>" /> <?php } |
The code above will add a text field that looks like this on the checkout page.
If you notice in the code there is a required field indicator. This simply adds an asterisk (*) next to the field’s title. This is optional, and if you don’t want your field to be required, just delete that span.
Validate Custom Required Field in Easy Digital Downloads
Since we have a required field, a red asterisk won’t ensure that the field is actually filled in when a customer checks out. It might help them to know that you are expecting an input in that field though, so it is a good thing to keep for your customer’s sanity.
Validating the fields in Easy Digital Downloads is a two part process. The first requires us to add to the list of required fields in Easy Digital Downloads and include the error message that should be displayed if the field is not filled in. The second part is to tell Easy Digital Downloads to check our custom field to see if it is filled in or not.
<?php /* * Sets our custom field to be required * Be sure to use the same name for your field in the $required_fields array */ add_filter( 'edd_purchase_form_required_fields', 'sd_edd_required_field' ); function sd_edd_required_field( $required_fields ) { $required_fields['sd_edd_checkout_field'] = array( 'error_id' => 'invalid_custom_field', 'error_message' => __( 'Please fill in the custom field' ) ); return $required_fields; } /* * Sets the error if the field is empty * Be sure to use the same name for your field in the $required_fields array */ add_action( 'edd_checkout_error_checks', 'sd_validate_custom_edd_checkout_field', 10, 2 ); function sd_validate_custom_edd_checkout_field( $valid_data, $data ) { if ( empty( $data['sd_edd_checkout_field'] ) ) { edd_set_error( 'invalid_custom_field', __( 'Please fill in the custom field' ) ); } } |
This will give the customer an error message if they fail to fill in the custom field in Easy Digital Downloads. This is especially useful because if the customer doesn’t know why the checkout isn’t processing, they may get frustrated and abandon their cart.
The custom field error will look something like this.
Saving Customer Input in Custom Easy Digital Downloads Checkout Fields
Adding a custom field to the checkout page is great, but it doesn’t help much if we don’t save what the customer enters.
Easy Digital Downloads has a filter that can add to the payment meta, which is what we’re going to use here.
<?php add_filter( 'edd_payment_meta', 'sd_save_custom_fields'); function sd_save_custom_fields( $payment_meta ){ if( isset( $_POST['sd_edd_checkout_field'] ) ){ //You may need to use a different function to safely store your customer's entry. This example uses sanitize_text_field() $payment_meta['sd_edd_checkout_field'] = sanitize_text_field( $_POST['sd_edd_checkout_field'] ); } return $payment_meta; } |
So, to recap, so far we have:
- Created a custom field on the Easy Digital Downloads checkout page.
- Validated that the custom field has been completed by the customer.
- Saved to the order’s payment meta.
The only thing left is deciding how to display the custom field’s data.
Displaying Custom Field Entry Data in Easy Digital Downloads
There are a couple areas I can think of where you might want to display your custom field’s data.
- Payment history order details (in the admin side).
- The order confirmation emails (admin and customer).
I’ll show you how to add the details to all three areas.
Add Custom Fields to Easy Digital Downloads Payment History Order Details
<?php /* * Add custom field to order details page. */ function sd_add_custom_field_to_order_details( $payment_meta, $user_info ) { $custom_field = isset( $payment_meta['sd_edd_checkout_field'] ) ? $payment_meta['sd_edd_checkout_field'] : 'none'; ?> <div class="column-container"> <div class="column"> <strong><?php _e( 'My custom field' ); ?>:</strong> <?php echo $custom_field; ?> </div> </div> <?php } add_action( 'edd_payment_personal_details_list', 'sd_add_custom_field_to_order_details', 10, 2 ); |
Adding this code will display the custom field’s entry at the bottom of the Customer Details box on the order details page.
Add Custom Fields to the Order Confirmation Email in Easy Digital Downloads
By adding the custom field to the order confirmation email in Easy Digital Downloads, we ensure that our customer has a record of what they entered. Also, the store’s admin will receive an email with the same information.
<?php /* * Add custom field to order confirmation email */ function sd_display_order_details_email( $email_body, $payment_id, $payment_data ){ $payment_meta = get_post_meta( $payment_id, '_edd_payment_meta', true ); if( array_key_exists( 'sd_edd_checkout_field', $payment_meta ) ){ $email_body .= '<strong>' . __( 'My custom field ') . ': </strong>' . $payment_meta['sd_edd_checkout_field'] . '<br />'; } return $email_body; } add_filter( 'edd_sale_notification', 'sd_display_order_details_email', 10, 3 ); add_action( 'plugins_loaded', 'sd_add_custom_field_to_order_email_filter' ); function sd_add_custom_field_to_order_email_filter(){ add_filter( 'edd_purchase_receipt_' . EDD()->emails->get_template(), 'sd_display_order_details_email', 10, 3 ); } |
You may want to style the custom field differently on your order confirmation emails.
Your customer will see something like this:
The site admin will get an email like this:
What Custom Fields Will You Use?
There are many reasons why someone would want to add a custom field to Easy Digital Downloads checkout. What will you use your custom field for?
Kevin says
I customized the code a bit but I am using your hooks & filters and it works perfectly.
I would still be searching through EDD source code looking for the right hooks and filters to use if it weren’t for your guide.
Thank you, Scott!
Cheers,
Kevin.
Scott DeLuzio says
Glad it helped Kevin!
Torsten Schmidt says
Hi Kevin,
Thamks for your well documented tutorial. You described exactly what I was looking for. I have a further question. Perhaps you can help me, or give me a hint where I can find the information.
I need to execute a php script after a successfull checkout. The php script needs as the url the email address from the custom field
Do you know how to solve this task?
Greetings from Germany
Torsten
Scott DeLuzio says
You might want to check out the After Payment Options hook. I think that will allow you to do what you’re looking for.
Torsten Schmidt says
Sorry Scott for my misspelling of your name 🙁
Alexandre says
Hi, thanks for this it’s perfect.
But how i can see this field in order details in my FES plugin for each vendor?
Michael says
Thank you, this is great.
I would have love to see how that will work for “variable pricing” so that the extra fields are ONLY displayed on the checkout page of the “price option the buyer chose”.
Scott DeLuzio says
It is possible to do that. I don’t have the code in tutorial format but it is in my Conditional Checkout Fields for EDD plugin.
ihsansahab says
how display conditional field checkout by download id or option pricing?
thanks Scott DeLuzio
Scott DeLuzio says
You can do that pretty easily with Conditional Checkout Fields for EDD.
Siddhesh says
Hello Scott,
Could you please let me know how I can remove the billing details from the checkout field in easy digital downloads
Scott DeLuzio says
Hi Siddhesh,
I think if you do that it will prevent customers from being able to complete payment on the site as most payment gateways require the billing details to be completed. If you only have free products on the site the billing fields will be removed by default. Were there specific billing fields that you wanted to hide?
Sebastián says
Hi,
Is it posible to add cuatom fields to edd free download addon?
Thanks
erik lindenburg says
Hi Scott,
This doesn’ seem to work in php8 anymore.
type E_ERROR Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, bool given in /var/www/vhosts/kinderburgemeesters.nl/httpdocs/wp-content/plugins/edd-gemeente/edd-gemeente.php:107
Stack trace:
if( array_key_exists( ‘sd_edd_gemeente’, $payment_meta ) ){
/*
* Add custom field to order confirmation email
*/
function sd_display_order_details_email( $email_body, $payment_id, $payment_data ){
$payment_meta = get_post_meta( $payment_id, ‘_edd_payment_meta’, true );
if( array_key_exists( ‘sd_edd_checkout_field’, $payment_meta ) ){
$email_body .= ‘‘ . __( ‘My custom field ‘) . ‘: ‘ . $payment_meta[‘sd_edd_checkout_field’] . ”;
}
return $email_body;
}
add_filter( ‘edd_sale_notification’, ‘sd_display_order_details_email’, 10, 3 );
add_action( ‘plugins_loaded’, ‘sd_add_custom_field_to_order_email_filter’ );
function sd_add_custom_field_to_order_email_filter(){
add_filter( ‘edd_purchase_receipt_’ . EDD()->emails->get_template(), ‘sd_display_order_details_email’, 10, 3 );
}
Do you have a solution? Thanks in advance!!!
Stuart Osborne says
I have just setup additional fields as per your tutorial you made this very easy and it works great, thank you.
One extra thing i would like to do is add some form of validation to stop people just typing in rubbish
i.e. making sure only alpha and not numeric characters are used or vice versa. is this possible?
Scott DeLuzio says
Yes, this is possible. You would have to add some checks in the code – where you put those checks, though, depends on the type of fields you use. If they’re required fields, you can add the check to the validation step that checks if the field is filled out, enabling the user to correct their entry and remove undesired characters before submitting the order. Otherwise, you can add it to the step that saves the custom field data to the order, which would prevent it from saving the custom field’s entry altogether while still allowing the order to be completed.
You’d probably need something like the
is_numeric()
orstr_contains()
functions to check whether or not there are numeric characters in the entry.