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:
- Identifying the correct meta key for your data.
- Choosing the appropriate action hook (e.g.,
wpo_wcpdf_after_order_data,wpo_wcpdf_after_billing_address). - 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.phpfile 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
-
Meta_key Array valuehttps://wordpress.org/support/topic/meta_key-array-value/
-
[NSFW] customer namehttps://wordpress.org/support/topic/customer-name-3/
-
Display Info from Order Items Metahttps://wordpress.org/support/topic/display-info-from-order-items-meta/
-
Include order status in PDFhttps://wordpress.org/support/topic/include-order-status-in-pdf/
-
How to display the phone numbers of Dokan vendorshttps://wordpress.org/support/topic/how-to-display-the-phone-numbers-of-dokan-vendors/
-
How to show coupon details in the emailhttps://wordpress.org/support/topic/how-to-show-coupon-details-in-the-email/
-
Display a due date 30 days end of month on an invoicehttps://wordpress.org/support/topic/display-a-due-date-30-days-end-of-month-on-an-invoice/
-
How to show order status on invoicehttps://wordpress.org/support/topic/how-to-show-order-status-on-invoice/
-
Transaction ID to be printed on invoicehttps://wordpress.org/support/topic/transaction-id-to-be-printed-on-invoice/
-
Output Subscriptions ID on the PDFhttps://wordpress.org/support/topic/output-subscriptions-id-on-the-pdf/
-
Add notes or custom texts to the invoiceshttps://wordpress.org/support/topic/add-notes-or-custom-texts-to-the-invoices/
-
How to add customer GST numberhttps://wordpress.org/support/topic/how-to-add-customer-gst-number/
-
Notes to Invoice and Packing slip named Serial numberhttps://wordpress.org/support/topic/notes-to-invoice-and-packing-slip-named-serial-number/
-
custom template customer detailshttps://wordpress.org/support/topic/custom-template-customer-details/
-
Card details on the order formhttps://wordpress.org/support/topic/card-details-on-the-order-form/
-
How to display loop of custom fields in invoice?https://wordpress.org/support/topic/how-to-display-loop-of-custom-fields-in-invoice/
-
Print out user rolehttps://wordpress.org/support/topic/print-out-user-role/
-
Invoice notes on pdfhttps://wordpress.org/support/topic/invoice-notes-on-pdf/
-
Add Status to CUstomized Templatehttps://wordpress.org/support/topic/add-status-to-customized-template/
-
Show order creation time in PDFhttps://wordpress.org/support/topic/show-order-creation-time-in-pdf/
-
I want to show Start date for active subscriptions in the invoicehttps://wordpress.org/support/topic/i-want-to-show-start-date-for-active-subscriptions-in-the-invoice/
-
Download Invoice with order idhttps://wordpress.org/support/topic/download-invoice-with-order-id/
-
how I can get the order ID for creating a shortcode?https://wordpress.org/support/topic/how-i-can-get-the-order-id-for-creating-a-shortcode/
-
Visible Coupon Codehttps://wordpress.org/support/topic/visible-coupon-code/
-
Add custom fields from external pluginhttps://wordpress.org/support/topic/add-custom-fields-from-external-plugin/