• Hi,
    I have successfully created a custom template by copying it into my child theme and personalizing it — everything works great.

    Now, I’d like to create a second different PDF template (like a voucher without price). Ideally, when I click a specific button (in the thankyou page after checkout), it would generate and download that second PDF instead of the default one.

    Could you please guide me on the best way to:

    1. Add and register an additional custom PDF template; and
    2. Trigger it with a separate button or link so I can generate that second document independently?

    Any example code snippets or documentation references would be really appreciated.

    Thanks a lot for your help and for the great plugin!

    Best regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Darren Peyou

    (@dpeyou)

    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?

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.