Back to Community

How to Display Custom Order and Product Data on Your WooCommerce PDF Invoices

Content

One of the most common tasks for WooCommerce store owners using the 'PDF Invoices & Packing Slips for WooCommerce' plugin is displaying custom data on their documents. This could be anything from a subscription's start date and a customer's GST number to a vendor's phone number from the Dokan plugin.

This need arises because the default plugin settings are designed to show core WooCommerce order information. Any additional data stored in custom fields, by other plugins, or in complex data structures (like serialized arrays) requires a specific approach to be retrieved and displayed.

Common Methods for Adding Custom Data

Based on community support threads, here are the most effective ways to add your custom information to PDF invoices and packing slips.

1. Using Action Hooks (The Most Common Method)

The plugin provides specific action hooks that allow you to inject custom content at various points in the PDF document. This is the recommended method for most use cases. The general process involves:

  1. Identifying the correct meta key for your data.
  2. Choosing the appropriate action hook (e.g., wpo_wcpdf_after_order_data, wpo_wcpdf_after_billing_address).
  3. Writing a small PHP code snippet to fetch and display the data.

Example 1: Displaying a Simple Order Meta Field (like a GST Number)

add_action( 'wpo_wcpdf_after_billing_address', function( $document_type, $order ){
    if ( $gst_number = $order->get_meta( 'gst_number' ) ) {
        echo "<div>GST Number: {$gst_number}</div>";
    } 
}, 10, 2);

Example 2: Displaying a Specific Value from a Serialized Array

If your data is stored as a serialized array, you need to unserialize it first to access individual values.

add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_my_custom_field', 10, 2 );
function wpo_wcpdf_my_custom_field ($document_type, $order) {
    if ( $document_type == 'invoice' ) {
        $payment_data = $order->get_meta('_pp_payment_data', true);
        if (!empty($payment_data)) {
            $data_array = maybe_unserialize($payment_data);
            $next_payment_date = $data_array['next_payment_date'] ?? ''; // Use array key
            if (!empty($next_payment_date)) {
                ?>
                <tr class="due-date">
                    <th>Due Date:</th>
                    <td><?php echo $next_payment_date; ?></td>
                </tr>
                <?php
            }
        }
    }
}

Example 3: Displaying Item-Specific Meta Data (e.g., from Order Items)

To show data specific to each product line item, use a hook like wpo_wcpdf_after_item_meta.

add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
    if ( empty( $item['item'] ) ) return;
    // Replace 'eap_course_data' with your item meta key
    if ( $custom_value = $item->get_meta( 'eap_course_data' ) ) {
        echo <<<HTML
        <div>
            <strong>Course Data:</strong> $custom_value
        </div>
        HTML;
    }
}

2. Using Conditional Checks for Complex Data

Sometimes you need to check for the existence of other plugins or data before displaying information. For instance, to show a Dokan vendor's phone number, you first need to check if the order has an associated vendor.

add_action( 'wpo_wcpdf_after_order_data', function( $document_type, $order ) {
    if ( $document_type == 'invoice' ) {
        $dokan_vendor_id = $order->get_meta( '_dokan_vendor_id', true );
        if ( !empty($dokan_vendor_id) ) {
            $vendor_phone = get_user_meta( $dokan_vendor_id, 'phone', true );
            if ( !empty($vendor_phone) ) {
                ?>
                <tr class="vendor-phone">
                    <th>Vendor Phone:</th>
                    <td><?php echo $vendor_phone; ?></td>
                </tr>
                <?php
            }
        }
    }
}, 10, 2 );

Important Notes and Troubleshooting

  • Where to Add Code: These code snippets should be added to your child theme's functions.php file or using a code snippets plugin. Never edit the parent theme's files directly.
  • Find the Correct Meta Key: The most crucial step is identifying the exact key under which your data is stored. Using a plugin like "Advanced Custom Fields" or simply inspecting your order's meta data in the database can help.
  • Check for Serialized Data: If your data is an array, you must use maybe_unserialize() to convert it back to a usable PHP array before you can access its values, as shown in Example 2.
  • Template Limitations: Some placeholders, like {{order_status}}, are only available if you are using the Premium Templates extension. For the free plugin, you must use a code snippet to display this data (e.g., $order->get_status()).

By leveraging these action hooks and understanding how your custom data is stored, you can significantly extend the functionality of your PDF invoices and packing slips to meet your specific business needs.

Related Support Threads Support