How to Add Custom Sections to Order Details in EDD 3.0

There is a lot of information stored about every order placed in an Easy Digital Downloads storefront. In 3.0, the Order Details screen has been completely updated to be easier to read and manage.

One big change is the addition of order sections, which allow necessary, but less immediate, data to be hidden on the initial screen load. Core sections included in Easy Digital Downloads are:

  • Customer (default)
  • Email
  • Address
  • Notes
  • Logs

Extension authors may want to add their own data to these sections as well. To do so, start with registering the custom section:

add_filter( 'edd_get_order_details_sections', 'prefix_add_custom_order_section', 10, 2 );
/**
 * Adds the custom details as an order section in EDD 3.0.
 *
 * @since 2.3.9
 * @param array  $sections The array of order sections.
 * @param object $order    The order object.
 * @return array
 */
function prefix_add_custom_order_section( $sections, $order ) {

    $sections[] = array(
        'id'       => 'prefix_order_section',
        'label'    => __( 'Custom Section' ),
        'icon'     => 'admin-multisite',
        'callback' => 'prefix_show_custom_order_section',
    );

    return $sections;
}

This will add your new custom section to the end of the array of sections (after Logs). Each section has four parameters:

  • id: The unique ID of the section
  • label: The label which will show in the navigation menu
  • icon: The dashicon to use with the navigation label
  • callback: the function which will output the section content

The next step is to build your prefix_show_custom_order_section function. This accepts one parameter, the order object. It would begin with something like this:

/** * Shows the custom order section in EDD 3.0.
 *
 * @param \EDD\Orders\Order object $order The order object.
 * @return void
 */
function prefix_show_custom_order_section( $order ) {
    printf( '<h3 class="hndle">%s</h3>', esc_html__( 'Custom Section Heading' ) );
    // Custom code for the section
}

Custom order data can be varied. EDD Simple Shipping, for example, registers two custom sections: one for the shipping address and one for tracking information. These are displayed as individual metabox style boxes in EDD 2.10, but as order sections in 3.0.

Screenshot of the Shipping order section.
Simple Shipping registers two custom order sections: Shipping and Tracking.

Note that in EDD 2.10, the parameter that’s available to a metabox is just the $payment_id, and in EDD 3.0, the entire order object is available. To be compatible with both versions of EDD, you may want to call a separate function from your prefix_show_custom_order_section function, and just pass the $order->id to it. It’s a little extra work to update the code to work with both the order sections and the edd_view_order_details_billing_after hook, which is what metaboxes in 2.10 would use, but it’s worth doing to seamlessly integrate into the new EDD.

Leave a Reply

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