Halloween Sale 15% off Ends in See the plans
PHP

"Print Invoice" Button in My Account Orders

Lets customers open a printable invoice for their paid orders from the My Account orders list, so they can file receipts without emailing you.

Don’t have WPIDE yet? Install it on your site

Code

add_filter( 'woocommerce_my_account_my_orders_actions', function ( $actions, $order ) {
    if ( $order->has_status( array( 'processing', 'completed' ) ) ) {
        $actions['wpide-invoice'] = array(
            'url'  => wp_nonce_url( add_query_arg( 'wpide_invoice', $order->get_id(), wc_get_page_permalink( 'myaccount' ) ), 'wpide_invoice_' . $order->get_id() ),
            'name' => 'Invoice',
        );
    }

    return $actions;
}, 10, 2 );

add_action( 'template_redirect', function () {
    if ( empty( $_GET['wpide_invoice'] ) ) {
        return;
    }

    $order_id = absint( $_GET['wpide_invoice'] );
    $nonce    = isset( $_GET['_wpnonce'] ) ? sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ) : '';

    if ( ! wp_verify_nonce( $nonce, 'wpide_invoice_' . $order_id ) || ! current_user_can( 'view_order', $order_id ) ) {
        wp_die( esc_html( 'Sorry, you cannot view this invoice.' ), 403 );
    }

    $order = wc_get_order( $order_id );

    if ( ! $order || ! $order->has_status( array( 'processing', 'completed' ) ) ) {
        wp_die( esc_html( 'Invoice not available.' ), 404 );
    }

    nocache_headers();
    $invoice = $order->get_meta( '_wpide_invoice_number' );
    ?>
    <!doctype html>
    <html lang="<?php echo esc_attr( get_bloginfo( 'language' ) ); ?>">
    <head>
        <meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex">
        <title><?php echo esc_html( 'Invoice #' . $order->get_order_number() ); ?></title>
        <style>body{font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;color:#111;max-width:760px;margin:2em auto;padding:0 1em}table{width:100%;border-collapse:collapse;margin:1.5em 0}th,td{text-align:left;padding:.5em;border-bottom:1px solid #ddd}.num{text-align:right}@media print{.no-print{display:none}}</style>
    </head>
    <body>
        <p class="no-print"><button onclick="window.print()"><?php echo esc_html( 'Print or save as PDF' ); ?></button></p>
        <h1><?php echo esc_html( get_bloginfo( 'name' ) ); ?></h1>
        <p><?php echo esc_html( 'Invoice for order #' . $order->get_order_number() ); ?><?php echo $invoice ? '<br>' . esc_html( 'Invoice number: ' . $invoice ) : ''; ?><br><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></p>
        <p><?php echo wp_kses_post( $order->get_formatted_billing_address() ); ?></p>
        <table>
            <?php foreach ( $order->get_items() as $item ) : ?>
                <tr><td><?php echo esc_html( $item->get_quantity() . ' × ' . $item->get_name() ); ?></td><td class="num"><?php echo wp_kses_post( $order->get_formatted_line_subtotal( $item ) ); ?></td></tr>
            <?php endforeach; ?>
            <?php foreach ( $order->get_order_item_totals() as $row ) : ?>
                <tr><th><?php echo esc_html( wp_strip_all_tags( $row['label'] ) ); ?></th><td class="num"><?php echo wp_kses_post( $row['value'] ); ?></td></tr>
            <?php endforeach; ?>
        </table>
    </body>
    </html>
    <?php
    exit;
} );

Notes

Customers can only open their own processing and completed orders.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.