Checkout Fields Manager - Getting Meta Data

The Checkout Fields Manager allows you to make custom fields for your checkout form. This makes it easy to gather data from your customers. You can see it easily on the Payment History page, but what if you want to have it printed on the receipt for the customer to see? This document will help with that.

Radio Buttons Example

In this example we're going to use a custom radio buttons field, titled "Are you happy with this purchase process?". The meta key for this field was automatically generated as are_you_happy_with_this_purchase_process, and we've simply made a Yes/No answer option. See below.

Since we're going to be rendering this information on the customer receipt, let's take a look at that template.  The template we want is called shortcode-receipt.php.  Read our docs on how to move this template to your theme.

Near the top of that file you'll find this code:

$payment = get_post( $edd_receipt_args['id'] );

which creates an object for this payment. In the rest of this file we can now find the ID for this payment under $payment->ID.

In the example above the Meta Key is are_you_happy_with_this_purchase_process. Now that we have both the ID and the Meta Key we can get the value with this code:

get_post_meta( $payment->ID, 'are_you_happy_with_this_purchase_process', true )

Note: the 'true' in the example above indicates that we're expecting a single value from get_post_meta(). Checkout Fields Manager returns a single value for all field types it offers.

Printing to the Receipt

In the above example we got the value we wanted, but now we want to render it nicely in receipts. In shortcode-receipt.php you'll find an html table with a CSS ID of edd_purchase_receipt. You can copy this table and change the values. Here's an example of a table header:

<table id="edd_purchase_receipt_custom_fields">
    <thead>
        <tr>
            <th colspan="2"><strong><?php _e( 'My Store Questions', 'easy-digital-downloads' ); ?></strong></th>
        </tr>
    </thead>
    <tbody>

Then we can print a table row for the above question, like this:

<tr>
    <td class="are_you_happy_with_this_purchase_process question"><strong><?php _e( 'Are you happy with this purchase process?', 'easy-digital-downloads' ); ?></strong></td>
    <td class="are_you_happy_with_this_purchase_process answer"><?php echo strip_tags( esc_html( get_post_meta( absint( $payment->ID ), 'are_you_happy_with_this_purchase_process', true ) ) ); ?></td>
</tr>

Multi-answer Example

Some field types allow for multiple answers from a customer. Check boxes allow the customer to select multiple things for example. This is still stored as a single item in meta data, it's a single array with multiple key/value pairs.  How it gets rendered is slightly different from above however.

Here's a custom field asking the customer what their favorite animal is:

Rendering it in our html table would look like this:

<tr>
    <td class="favorite_animal question"><strong><?php _e( 'Favorite animal?', 'easy-digital-downloads' ); ?></strong></td>
    <?php   
	// create an empty variable for the answers
        $animals = '';

	// loop through the answers, creating list items
        foreach ( get_post_meta( $payment->ID, 'favorite_animal', true ) as $animal ) {
            $animals .= '<li>' . $animal . '</li>' . "\n"; 
        }

	// print out the table row, wrapping the results in unordered list tags, and escaping with wp_kses_post()
    ?>
    <td class="favorite_animal answer"><ul><?php echo wp_kses_post( $animals ); ?></ul></td>
</tr>

As you can see it's only slightly more complicated than the first example.

Wrapping Up

Putting together the two code samples from above we end up with something like this:

<table id="edd_purchase_receipt_custom_fields">
    <thead>
        <tr>
            <th colspan="2"><strong><?php _e( 'My Store Questions', 'easy-digital-downloads' ); ?></strong></th>
        </tr>
    </thead>
    <tbody>

    <tr>
        <td class="are_you_happy_with_this_purchase_process question"><strong><?php _e( 'Are you happy with this purchase process?', 'easy-digital-downloads' ); ?></strong></td>
        <td class="are_you_happy_with_this_purchase_process answer"><?php echo strip_tags( esc_html( get_post_meta( absint( $payment->ID ), 'are_you_happy_with_this_purchase_process', true ) ) ); ?></td>
    </tr>

    <tr>
        <td class="favorite_animal question"><strong><?php _e( 'Favorite animal?', 'easy-digital-downloads' ); ?></strong></td>
        <?php   
            // create an empty variable for the answers
            $animals = '';

            // loop through the answers, creating list item
            foreach ( get_post_meta( $payment->ID, 'favorite_animal', true ) as $animal ) {
                $animals .= '<li>' . $animal . '</li>' . "\n"; 
            }

        // print out the table row, wrapping the results in unordered list tags, and escaping with wp_kses_post()
        ?>
        <td class="favorite_animal answer"><ul><?php echo wp_kses_post( $animals ); ?></ul></td>
    </tr>
    </tbody>
</table>

Which will look like this:

Key Points

  • To render Custom Fields answers on your receipts, copy the shortcode-receipt.php file to your theme and put your code in there
  • Getting the data is simply doing a get_post_meta() call on the Meta Key provided in you admin area
  • All keys from Custom Fields Manager are stored as singles in the post_meta table.