Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Adding Custom Fields to the Easy Digital Downloads Checkout

Posted on May 7, 2012 by Pippin in Action and Filter Hooks, Intermediate, Tutorials, Writing Plugins 15 Comments
Home» Tutorials » Action and Filter Hooks » Adding Custom Fields to the Easy Digital Downloads Checkout
Tweet
Love It - 0

Easy Digital Downloads is a plugin I have been building for selling digital downloads through WordPress. It is completely free and very flexible. Due to a large number of actions hooks and filters, there are many ways that you can customize the plugin to best suit your needs. This tutorial will walk you through the process adding custom fields to the checkout screen, and also storing the information received through the fields.

The fields we will be adding are additional “personal info” fields. We will add one for the buyer’s phone number, and one for the buyer’s company name. These two fields can be substituted for any kind of field you want as they are just examples. The final result will look like this: (not the phone and company fields are what we are adding):

Easy Digital Downloads incorporates a lot of actions hooks and filters that we can use for modifying the plugin’s behavior. I’ve talked about this topic quite a bit in my Plugin Thoughts, and it’s this system that we are going to be working with now.

Each of the modifications we make will be done either with the add_action() function, or with the add_filter() function.

Adding the Fields to the Checkout Form

The fields for phone number and company name are nothing more than plain HTML input fields. We can output the fields by tying into the “edd_purchase_form_user_info” hook, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
// output our custom field HTML
function pippin_edd_custom_checkout_fields() {
	?>
	<p>
		<input class="edd-input required" type="text" name="edd_phone" id="edd-phone" placeholder="<?php _e('Phone Number', 'edd'); ?>" value=""/>
		<label class="edd-label" for="edd-phone"><?php _e('Phone Number', 'edd'); ?></label>
	</p>
	<p>
		<input class="edd-input" type="text" name="edd_company" id="edd-company" placeholder="<?php _e('Company name', 'edd'); ?>" value=""/>
		<label class="edd-label" for="edd-company"><?php _e('Company Name', 'edd'); ?></label>
	</p>	
	<?php
}
add_action('edd_purchase_form_user_info', 'pippin_edd_custom_checkout_fields');

Each field gets a name and ID attribute. We also set the HTML5 placeholder for each field; this acts as the default text for the field, which is displayed before the user inputs any information. For the phone number field, I have also given a class called “required”. Doing this makes the jQuery validation for the checkout page recognize this field as a required field, and the checkout form will not be able to be submitted until it has been filled out.

Our checkout form will not look like the screenshot above.

Validating the New Checkout Fields

When the checkout form is submitted, we need to validate the data submitted and make sure everything is okay. We have added “required” class to the phone number, which takes care of the jQuery validation, but that doesn’t apply if the site owner has disabled jQuery validation in the Easy Digital Downloads settings, so we need to do some server side validation as well with PHP.

Since only our Phone Number field is required, we will only validate the data for that field. EDD (Easy Digital Downloads) has an action hook called “edd_checkout_error_checks”, which we can use to intercept the checkout form data before it is sent off to the payment gateway. Our simple validation function looks like this:

1
2
3
4
5
6
7
8
9
<?php
// check for errors with out custom fields
function pippin_edd_validate_custom_fields($data) {
	if(!isset($data['edd_phone']) || $data['edd_phone'] == '') {
		// check for a phone number
		edd_set_error( 'invalid_phone', __('You must provide your phone number.', 'pippin_edd') );
	}
}
add_action('edd_checkout_error_checks', 'pippin_edd_validate_custom_fields');

This function simply checks to make sure that the phone number field has been sent and that it’s not blank. If the field is not present, or it is blank, then register an error using the edd_set_error() function. Doing so causes the checkout process to return back to the checkout page, instead of moving on to the payment gateway. It also causes an error to be displayed on the checkout page.

With this validation function, the purchase will not be able to be completed until the phone number field has been filled out.

Storing Our Custom Field Data

In order for the custom fields to be useful, we need to store them somewhere. There are several methods we could use for storing the data. For example, we could store it in the customer’s user meta, but in this case, it will be better if we store the phone and company fields in the payment meta. Doing so will allow us to go to the Payment History page, click “View Order Details” for a purchase, and then see the Phone and Company fields displayed along with the buyer’s other Personal Info, as shown in the screenshot below:

The code needed to store our phone and company fields in the payment meta is quite simple. For adding and validating the fields, we used action hooks, now, however, we are going to use a filter provided by EDD. This filter will take the additional fields and add them to the payment meta generated when a purchase is made. The filter is called “edd_payment_meta”, and the function looks like this:

1
2
3
4
5
6
7
8
<?php
// store the custom field data in the payment meta
function pippin_edd_store_custom_fields($payment_meta) {
	$payment_meta['phone'] = isset($_POST['edd_phone']) ? $_POST['edd_phone'] : '';
	$payment_meta['company'] = isset($_POST['edd_company']) ? $_POST['edd_company'] : '';
	return $payment_meta;
}
add_filter('edd_payment_meta', 'pippin_edd_store_custom_fields');

When a payment is made now, the phone and company fields will be stored. The only thing we need to do now is display the stored values. We are going to add the additional fields to the “View Order Details” popup that is shown in the screenshot above.

Displaying the Custom Payment Meta

To show the custom phone and company meta, we use another action hook called “edd_payment_personal_details_list”. This hook is quite simple:

1
2
3
4
5
6
7
8
9
10
11
<?php
// show the custom fields in the "View Order Details" popup
function pippin_edd_purchase_details($payment_meta, $user_info) {
	$phone = isset($payment_meta['phone']) ? $payment_meta['phone'] : 'none';
	$company = isset($payment_meta['company']) ? $payment_meta['company'] : 'none';	
	?>
	<li><?php echo __('Phone:', 'pippin') . ' ' . $phone; ?></li>
	<li><?php echo __('Company:', 'pippin') . ' ' . $company; ?></li>
	<?php
}
add_action('edd_payment_personal_details_list', 'pippin_edd_purchase_details', 10, 2);

The functions checks to make sure that both fields are present (and sets them each to “none” if not present), and then outputs them as list items.

That’s it! We have now successfully added custom fields to the checkout form for Easy Digital Downloads, stored the field values, and displayed them in the admin.

This only touches on the customizations you can make to Easy Digital Downloads. While not complete, checkout the Developer’s API for additional reference.

Tweet Follow @pippinsplugins
add_action, add_filter, easy digital downloads

15 comments on “Adding Custom Fields to the Easy Digital Downloads Checkout”

  1. Michael Martin says:
    May 11, 2012 at 5:57 am

    Very nice tutorial Pippin, it’s awesome to see that Easy Digital Downloads can be extended so far with this sort of functionality. It especially rocks how even the server-side validation has been covered here, love it! :)

    Reply
    • Pippin says:
      May 11, 2012 at 10:55 am

      Thanks Michael! If you ever get the chance to give the plugin a real trial run (or real run), I’d love to hear your feedback.

  2. Frank says:
    January 19, 2013 at 10:05 am

    Hey,

    really love your plugin but like some other folks, i’d really love to add a few fields for address so I can also sell physical products. i’ve tried the above, but i just cant figure out where all this stuff goes in the code.

    any other tutorials you have on this? or possible the PHP files with these edits included?

    thanks!

    Reply
    • Pippin says:
      January 21, 2013 at 3:28 pm

      The code needs to go inside of a custom plugin. The easiest way to create a custom plugin is to install the “Pluginception” plugin.

  3. Frank says:
    January 22, 2013 at 3:54 pm

    ok. i have that, but i keep getting errors when i try to save the new plugin. “This plugin has been deactivated because your changes resulted in a fatal error.” Would it be possible for you to email me a text file with the code how it should appear in the pluginception window? (the above example is fine) THANKS

    Reply
    • Pippin says:
      January 22, 2013 at 4:21 pm

      Paste the code that you have into snippi.com and share the link.

  4. Frank says:
    January 23, 2013 at 9:16 am

    http://snippi.com/s/ld9mhab

    Reply
    • Pippin says:
      January 23, 2013 at 11:27 am

      Use this version: http://snippi.com/s/4621fos

  5. Frank says:
    January 23, 2013 at 2:14 pm

    I appreciate all your help. now it says:

    Parse error: syntax error, unexpected T_DNUMBER in /home2/executeo/public_html/crisiscenterpro/wp-content/plugins/custom-edd/custom-edd.php on line 22

    Reply
    • Pippin says:
      January 23, 2013 at 5:15 pm

      What’s on line 22? The code I’m looking at definitely doesn’t have an error on that line.

  6. Frank says:
    January 23, 2013 at 7:05 pm

    Got it! Thank you for all your help. And once again, awesome plugin!

    Reply
    • Pippin says:
      January 23, 2013 at 8:21 pm

      Glad to help!

  7. Johannes says:
    February 27, 2013 at 6:23 am

    I realy like your plugin! everything works fine!

    How to display the costom fields in the order email?

    Reply
    • Pippin says:
      February 28, 2013 at 4:08 pm

      I can point you in the right direction if you’re really comfortable with PHP.

  8. Johannes says:
    March 2, 2013 at 8:21 am

    got it working for me just with the Order Details view. Thanks!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Restrict Content Pro – Member Discounts for Easy Digital Downloads
  • Easy Digital Downloads – One Year Old
  • Easy Digital Downloads v1.5 Released
  • Crucial Security Flaw Discovered and Fixed
  • Plugin Development 101 – Intro to Actions

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • Use Easy Digital Downloads and iOS? Make sure you checkout our mobile app: https://t.co/mvv0a9g7tq
    May 21, 2013
  • @gmwdesign post a ticket in the forums and I&#039;ll help as soon as able
    May 21, 2013
  • RT @WordCampSF: Reminder that WordCamp San Francisco tickets go on sale this Thurs at 10am PT. For ticket details: http://t.co/psoTDMrFpn #…
    May 21, 2013

Topics

featured get_user_meta the_content shortcodes contextual help Sugar Event Calendar add_options_page campaign monitor meta box Rémi Corson add_shortcode register_setting wp_enqueue_script authors attachment image plugin forms login short codes do_action Related posts mail chimp apply_filters bbpress comments recent posts post types short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins