This tutorial will show you a start to finish example on adding a custom field to the checkout screen in Easy Digital Downloads, right through to displaying the custom field in email notifications.

Here’s a run down on what we’ll be covering:

  1. Adding a phone number field to the checkout
  2. Making the phone number field required
  3. Setting an error when the phone number field is not filled out
  4. Storing the phone number into the payment meta
  5. Storing the phone number into the wp_usermeta table (so we can use it on the WP edit profile screen)
  6. Adding the phone number to the user’s “Contact Info” section on their WordPress profile page and allowing it to be modified.
  7. Adding the phone number field to the page where [[edd_profile_editor]] is used, making it required, and allowing the customer to modify it.
  8. Pre-populating the phone number field at checkout if the user if logged in and already has a phone number
  9. Adding the customer’s phone number to the “view order details” screen and being able to update it like the other fields
  10. Adding a new {phone} email tag so you can display the phone number in the email notifications (standard purchase receipt or admin notification)
The "phone" field added to the checkout screen in Easy Digital Downloads
The “phone” field added to the checkout screen

15804-view-order-details

 

The new {phone} email tag for use within the emails in Easy Digital Downloads
The new {phone} email tag for use within the emails

Copy and paste the following code (mind the opening PHP tag) into your child theme’s functions.php or custom plugin.

55 comments

  1. This is great and just what I was looking for! One additional question, is there a way to add these new fields to the payment history table? Or for that matter is there any way to add/remove any of the default columns that appear via ‘edd_payments_table_columns’? Thanks again!

    1. Hi Mike, are you familiar with how to use filters? There’s a edd_payments_table_columns filter that you can use to add a new column. See the get_columns function in class-payments-table.php and you’ll see the filter at the bottom. You can also use this to remove the ones you don’t want.

      1. I am a bit of a PHP newbie, but I have figured out how to use the filter to add the new columns to the payment history table. However, only the heading of the columns show for the new ones I have added. I assume that is because I need to somehow use the edd_payments_table_column (singular) filter but I just cannot figure out how. In my use case, I used your code above to add both a reference (how did you hear about me) field and a feedback textarea field to the checkout page. I would like to show the contents of both in separate columns for each purchase in the payment history table. Happy to provide any additional information! Thanks again so much in advance.

        1. Can you show me your full code (paste to a site like gists.github.com or pastebin.com) and show me a screenshot of what you see?

          1. I just used your the contact form to send you the info you requested, I just didn’t want to post it publicly. Thanks again for all your help!

  2. I just updated to edd 2.0 and the call to: edd_add_email_tag

    Is no longer valid.

    Is this an intended side effect, or possibly a bug?

    Any insights?

  3. Problem found and corrected…..

    While it seems obvious now, it didn’t earlier….

    edd_add_email_tag was invalid because when EDD 2.0 did the upgrade the plugin was deactivated.

    I didn’t notice.

    Sorry about the noise.

    1. I’ve updated the code above to check for the edd_add_email_tag function so there won’t be any issues when upgrading.

  4. I’ve got the same problem with the edd_add_email_tag. I’ve commented it out so it works for now, but it would be nice to put it back in. Not sure what’s happened there?

    1. When EDD is deactivated, edd_add_email_tag is not present.

      The tutorial doesn’t (or didn’t last I checked) have any check to verify that the class is loaded or the fuction is defined.

      When it’s not defined, my experience is that EDD doesn’t activate properly during an upgrade because the function missing is throwing an error and stopping.

      Hope this is useful….

      BTW

    2. I’ve updated the code above to check for the edd_add_email_tag function so there won’t be any issues when upgrading.

  5. I’ve come up with a problem again… I would like to add the action to before the personal info fields instead of at the bottom of form.

    I’ve changed the code to use the edd_before_purchase_form and it shows up nicely, but when I submit the form, the comment does not show up on the campaign.

    What else do I need to change?

    Thanks!

    1. Hi Karl, not sure what you are intending to do. The code above doesn’t show anything on campaigns, it’s only rigged to show a field at checkout.

  6. I copied the code above to add an additional field to the membership form at checkout. It appears in the form just fine, but the data that it collects does not appear in the user profile. I have tried it with the original “phone” code and with a modified version for “Affiliation” (just changing the labels, not the hooks. Same results.

    Any suggestions? Try it out for yourself by downloading one of the free PDF files: http://ibstpi.org/download-center-free/ and then clicking on “Payment History” in the header nav. All WP core and plugins fully updated.

    1. If you take the code above, and use as-is, does it work ok? If it does, you’ve likely made a mistake in your code. If it only doesn’t work when modified, paste your full code to pastebin.com and I’ll take a look.

      1. Sorry to sound like a schmuck, but as I said in my original post, I tried it both ways: as the original code and as modified code, changing only the labels.

        1. Not at all. Do you mind giving me admin access? I’ll take a look and see if I can figure out why. You can email me using the contact page above.

          1. Your feedback comment from testing it said that there is no checkout process. Incorrect. You did not get the checkout page because you were logged in. Try it again but not logged in. You will get the checkout page asking to either log in or create an account. I currently have the code in place that requests Affiliation because the site is live, but I assure you that it is the same code as the Phone example, with just the labels changed.

  7. Hi ,
    i managed to add custom field in Form Submission EDD i.e Enter Commission by using hook , But some how i am not able to get “custom_field” value , as blank value is always inserted in my ‘_edd_commission_settings’ meta_key field , however i have managed to insert correct values for other 2 fields i.e ‘user_id’, ‘type’. Any help would be appreciated. Below is sample code :-

    — Code Starts —

    function your_function_name( $form_id, $post_id, $form_settings ) {
    $value = ”;

    if ( $post_id ) {
    $value = get_post_meta( $post_id, ‘_edd_commission_settings’, true );
    }
    ?>

    Enter Commission

    $_REQUEST[‘custom_field’] ,
    ‘user_id’ => $current_user->ID,
    ‘type’ => ‘percentage’
    );

    update_post_meta( $post_id, ‘_edd_commission_settings’, $commission );

    }

    add_action( ‘my_brand_new_hook’, ‘your_function_name’, 10, 3 );

    — Code Ends —

  8. It’s disappointing that we have to go through this extra to add the fields to the e-mail notices. My client that I’m running the site for is thinking the system is just broken, and I didn’t realize when I recommended the purchase of this module that it wouldn’t do what she was asking for. If it’s going to be a while to add in e-mail functionality out of the box, please add something clearly on the page for this mod so buyers are aware. I never thought the look for the functionality because it seemed like such an obvious thing to have.

    1. Hi Dan,

      Can you elaborate on what is missing? There are many email template tags already available for EDD. This tutorial is what you would follow if you wanted to create your own.

  9. Phone number is not recorded in the profile WordPress. Field of e-mail, name, last name missing icon “star”. Ultimately when choosing a new phone is not stored in the field.

  10. Icon “asterisk” I understood, it was necessary to add the required in edd_purchase_form_required_fields. Remains unresolved question is why the phone is not displayed in the profile WP

    1. Can you disable all other plugins except EDD and the code above and test again? I’ve tested it multiple times and works fine.

  11. Hi there,

    Great instructions! Just one issue: Through EDD Misc. settings I enabled the ‘show register form’ on checkout page option: http://crossaid.nl/doneren/. Because of this, the Contact Number field shows up twice. Once in the ‘Personal Information’ form and again in the ‘Create an account (optional)’ form.

    I only want to add the Contact Number field in the Personal Information form. How do I do this? I’m using a custom plugin where I pasted the code above.

    Thanks in advance.

      1. Hi,

        Thank you for the code. It is working very well.
        I have the same issue with Tiho. Contact Number is shown twice, in Personal Info and Create an Account. And in Create an Account there is no space after Password Again field. Is it possible to get a solution?

        Best Regards,

  12. Hi there,

    I tried the code above and it works like a charm. Only problem is that I enabled to forms on checkout (1 for Personal Info and 1 for the option to Create an Account). Because of this the custom fields created through this code, are shown twice.

    How do I make the fields created only show in 1 of these forms?
    You can see on http://crossaid.nl/doneren/ that Phone Number, Address and Country are in both forms.
    How do I only make them show in the Personal Info section?

    Thanks in advance,

    Tim

    1. Hi Tim, can you show me a product page URL? I have to add something to the cart before I can see your checkout cart. Can you show me your full code that you have implemented? Does the code above when used on it’s own show the phone field in both places?

  13. Hi Sumobi,

    You can add a product yourself. It’s on test mode. If you click the donate now! button and just typ a random number you’ll get redirected to the checkout page. The whole site is on testmode.

    The code I have implemented (through a custom plugin) is this: http://pastebin.com/7Zh9dE1s
    I don’t really know how to test this on it’s own, any idea how I do that?

    Thanks in advance,

    Tim

  14. thank you
    but i have two problems:
    1. i don’t want phone Filide have required
    2. i want show phone number in output backup csv

  15. I love the code, thanks. Now I’m wondering if it is possible to write the phone number into the csv file generated by reports->export->Export Payment History.

    This code i’ve brewed up appends the phone as a column, but it dosn’t contain any data. Do you have any suggestions?
    function generate_csv_cols( $columns) {
    $columns[‘phone’] = ‘Phone’;
    return $columns;
    }
    add_filter( ‘edd_export_csv_cols_payments’, ‘generate_csv_cols’ );

    //Then filling in the phone data
    function my_child_theme_edd_sale_cvs( $data ) {
    foreach ( $data as $key => $d ) {
    $phone = get_user_meta( $data[$key], ‘_edd_user_phone’, true );
    $data[$key][‘phone’]=$phone ;
    }
    return $data;
    }

    add_filter( ‘edd_export_get_data_payments’, ‘my_child_theme_edd_sale_cvs’ );

  16. thanks for this peace of code. did work. but how can i show the new additional fields on the register-page (where i use the shortcode [edd_register]). on the checkout-page it works like charm and it creates all the field in the database. i tried to add it to the register-only-form by this code: add_action(‘edd_register_form_fields_before_submit’, ‘sumobi_edd_display_checkout_fields’);

    it shows me the new field(s), but when i create a new user the new fields are not created in database. can you help me with this? thanks so much!

  17. and another problem i have is that the new fields are not updated / created in the wordpress usermeta database.

  18. sory but i forget another point. the new fields are only stored in the wp-usermeta when the purchase is completed. how can i chance the code so that the fields are created/updated if new account is created, whether if the purchase was completed or not.the account will be created in every way. so it would be cool if the new fields are created in usermeta, too.

  19. We would like to make the customers email adress to be the “From” adress in the sales notifications Emails (to be precise the Email that we recieve, not the one that the client recieves), so when we get Email notifications from sales, it looks like it comes from the customer, not just from the website adress. The reason for this is to have these Emails automatically attached to the right contacts in our CRM system. This CRM system can automatically attach an incoming Email to the appropriate contact, based on his Email adress … so if we can make these sales Emails look as if they were sent by the customer, all will be smoothly attached to his record in the CRM system.

  20. Thanks for this tut Andrew!

    Just a note, I think your $required_fields filter needs to add the key in it, otherwise it disables all of the required fields.

    $required_fields = array(
    ‘edd_phone’ => array(
    ‘error_id’ => ‘invalid_phone’,
    ‘error_message’ => ‘Please enter a valid Phone number’
    ),
    );

    should be

    $required_fields[‘edd_phone’] = array(
    ‘error_id’ => ‘invalid_phone’,
    ‘error_message’ => ‘Please enter a valid Phone number’,
    );

    At least that’s what I’m seeing in playing with this now.

  21. Thanks for the code. Now I am able to add a Phone Custom field.
    1. But the “First Name” which was required before doesn’t have the * anymore and is not required.
    How do I add a phone field without disturbing any of the original fields? Please help.

    2. How do I pass this phone field to the Payment gateway which required a phone number to be mandatory. How do I include this in $purchase_data? I am using Edd PayU India gateway for PayU Money.

    Please help.

  22. I added your code into a plugin file and also copied the shortcode-profile-editor.php file into my plugin folder (I don’t have any child theme).

    I replaced “phone” with “tva”, and

    It seems working ok, but I need to add this extra checkout field into my pdf invoices template. You say we can do it, but I don’t understand how to proceed. Can you please provide me a piece of code for that ? Here is what I added in my default-template (not working):

    if(!empty( $payment_meta[‘tva’] ) ) {
    $chris_tvanumber = $payment_meta[‘tva’];
    $eddpdfi_pdf->Cell( 0, 6, $chris_tvanumber, 0, 2, ‘L’, false );
    }

  23. Thank you for this snippet! IT worked great and I was able to add two extra fields to my cart page, but I have a particular request, if you can help..

    I have a select field with default values, but depending on the products in the cart, I may need additional options to the select (in practice, this happens because we may have different location for the pick up).

    Is there a trick to get the products IDs and conditionally show custom options to the select?

  24. Hello! I am a totally newbie on PHP. Sorry!

    I’ve pasted your code on shorcode-profile-editor.php, but when loading the checkout page it shows on top of the form, between payment method and First name (outside fieldset). I’ve tried some places inside the code but it allways appears on top of the form.

    Please, where do I have to paste the code?

    Thank you!

Comments are closed.