Hi @minamt,
I think the way to go is to dynamically change the path of the custom template based on certain conditions.
This filter should help: <code>wpo_wcpdf_template_file</code>. Its arguments are:
– A string for the file path.
– A string for the document type.
– A WooCommerce order object.
Reference: PDF Filter Hooks – WP Overnight documentation
The link contains a list of all the filter hooks we use in the free plugin. There is no built-in functionality for adding a button.
Thread Starter
minamt
(@minamt)
Hi,
Thank you for your reply, I’ve already used the wpo_wcpdf_template_file filter but it shows me “Invalid order” instead of the pdf template even though the order number is correct:
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;
}
Am I using the filter wrong?