I’m using the WooCommerce PDF Invoices & Packing Slips plugin by WP Overnight.
I’ve successfully:
- Created a second custom PDF template (a voucher version without prices).
- Added a button on the Thank You page to download that second template.
Everything appears to be set up correctly — but when I click the new button, I get this error:
Invalid Order The order ID being passed is correct (I double-checked the value and it matches the order that just completed).
What I’ve done:
Created the second template under my child theme: wp-content/themes/child-theme/woocommerce/pdf/voucher.php And registered it via filter adding a custom button on Thank you page:
add_action('woocommerce_thankyou', 'add_voucher_pdf_button', 20);
function add_voucher_pdf_button($order_id) {
if (!$order_id) return;
$order = wc_get_order($order_id);
if (!$order) return;
// Nonce for security, must match the document type (invoice)
$nonce = wp_create_nonce('generate_wpo_wcpdf');
// Build the correct AJAX URL
$pdf_url = add_query_arg(array(
'action' => 'generate_wpo_wcpdf',
'document' => 'invoice', // keep as invoice, since voucher isn’t a registered type
'order_ids' => $order->get_id(),
'voucher_pdf' => 1,
'_wpnonce' => $nonce,
), admin_url('admin-ajax.php'));
echo '<a href="' . esc_url($pdf_url) . '" target="_blank" class="button alt" style="margin-top:20px;">Download Voucher PDF</a>';
}
add_filter('wpo_wcpdf_template_file', 'load_voucher_template_conditionally', 10, 3);
function load_voucher_template_conditionally($template, $type, $order) {
if (!empty($_GET['voucher_pdf']) && $_GET['voucher_pdf'] == 1) {
$voucher_template = get_stylesheet_directory() . '/woocommerce/pdf/voucher-template/' . basename($template);
if (file_exists($voucher_template)) {
return $voucher_template;
}
}
return $template;
}
Any insight or example on how to do this correctly?
1 Answer 1
First you need to register your custom PDF type with WP Overnight so it’s a valid document:
add_filter( 'wpo_wcpdf_document_types', function( $document_types ) {
$document_types['voucher'] = array(
'title' => __( 'Voucher', 'your-textdomain' ),
'description' => __( 'Custom voucher PDF without prices', 'your-textdomain' ),
'template' => 'voucher.php',
'folder' => 'woocommerce/pdf',
'filename' => 'voucher-{order_number}.pdf',
'attach_to_email' => false,
'paper_size' => 'A4',
);
return $document_types;
});
Next, your button should pass the new document type:
add_action('woocommerce_thankyou', 'add_voucher_pdf_button', 20);
function add_voucher_pdf_button($order_id) {
if (!$order_id) return;
$order = wc_get_order($order_id);
if (!$order) return;
$nonce = wp_create_nonce('generate_wpo_wcpdf');
$pdf_url = add_query_arg(array(
'action' => 'generate_wpo_wcpdf',
'document' => 'voucher', // <-- new document type
'order_ids' => $order->get_id(),
'_wpnonce' => $nonce,
), admin_url('admin-ajax.php'));
echo '<a href="' . esc_url($pdf_url) . '" target="_blank" class="button alt" style="margin-top:20px;">Download Voucher PDF</a>';
}
Finally, place your template file here:
wp-content/themes/your-child-theme/woocommerce/pdf/voucher.php
Hope it works for you!