Download link on the Thank You page

You can add a download link to the PDF invoice on the WooCommerce “Thank You” page with a small code snippet (filter).
Read this if you haven’t used WordPress filters / edited functions.php before!

Logged-in users only #

Due to security restrictions, only registered/logged-in users will normally be able to download their invoices on the “Thank You” page, this can be achieved using the code snippet below. If you want to show the link for both Logged-in users and guest users, see below under Enabling guest access.

add_filter('woocommerce_thankyou_order_received_text', 'wpo_wcpdf_thank_you_link', 10, 2);
function wpo_wcpdf_thank_you_link( $text, $order ) {
    if ( is_user_logged_in() ) {
        $pdf_url = wp_nonce_url( add_query_arg( array(
            'action'        => 'generate_wpo_wcpdf',
            'document_type' => 'invoice',
            'order_ids'     => $order->get_id(),
            'my-account'    => true,
        ), admin_url( 'admin-ajax.php' ) ), 'generate_wpo_wcpdf' );
        $link_text = 'Download a printable invoice / payment confirmation (PDF format)';
        $text .= sprintf( '<p><a href="%s">%s</a></p>', esc_attr( $pdf_url ), esc_html( $link_text ) );
    }
    return $text;
}

Enabling guest access #

If you want guest users (as well as logged-in users) to be able to download their invoices on the “Thank You” page too (without being logged in), you need to select the Guest or Full options under WooCommerce > PDF Invoices > Advanced > Settings > Document link access type.

You can then use a slightly altered version of the code above, replacing the nonce with a unique order_key value that acts as a password:

add_filter( 'woocommerce_thankyou_order_received_text', 'wpo_wcpdf_thank_you_link', 10, 2 );
function wpo_wcpdf_thank_you_link( $text, $order ) {
    $pdf_url = add_query_arg( array(
        'action'        => 'generate_wpo_wcpdf',
        'document_type' => 'invoice',
        'order_ids'     => $order->get_id(),
        'order_key'     => $order->get_order_key(),
    ), admin_url( 'admin-ajax.php' ) );
    $link_text = 'Download a printable invoice / payment confirmation (PDF format)';
    $text .= sprintf( '<p><a href="%s">%s</a></p>', esc_attr( $pdf_url ), esc_html( $link_text ) );

    return $text;
}

Alternatively, you can hook this text to the woocommerce_thankyou action, see this thread on the support forum.