79770417

Date: 2025-09-20 16:19:58
Score: 0.5
Natty:
Report link

Yes it’s possible to add a Download Invoice button next to the “View” button in the WooCommerce My Account → Orders table.

By default, WooCommerce doesn’t generate invoices in PDF. For that, you’ll need an invoice plugin (e.g., WooCommerce PDF Invoices & Packing Slips – the most popular free one). That plugin registers a function to generate/download invoices, and we can hook into it to add a button.

Here’s a general snippet for the free WooCommerce PDF Invoices & Packing Slips plugin:

/**
 * Add Download Invoice button next to View button in My Account > Orders
 */
add_filter( 'woocommerce_my_account_my_orders_actions', 'add_invoice_download_button', 10, 2 );

function add_invoice_download_button( $actions, $order ) {
    // Make sure WooCommerce PDF Invoices & Packing Slips is active
    if ( class_exists( 'WPO_WCPDF' ) ) {
        $actions['invoice'] = array(
            'url'  => wp_nonce_url(
                add_query_arg( array(
                    'pdf_invoice' => 'true',
                    'order_ids'   => $order->get_id(),
                ), home_url() ),
                'generate_wpo_wcpdf'
            ),
            'name' => __( 'Download Invoice', 'woocommerce' ),
        );
    }
    return $actions;
}

And if you want without a plugin, out of the box, WooCommerce does not generate PDF invoices — it only stores order data in the database. A PDF needs to be dynamically created (HTML → PDF), which requires a library like Dompdf, TCPDF, or mPDF. That’s why most people use plugins, because they bundle those libraries and handle formatting.

But yes, it’s possible without a plugin, if you’re okay with some custom coding.

Here’s the flow we’d need:

  1. Create a custom endpoint (like ?download_invoice=ORDER_ID).

  2. Fetch the WooCommerce order data.

  3. Generate a PDF (using PHP library like Dompdf or FPDF).

  4. Stream it as a download.

  5. Add a "Download Invoice" button in the My Account Orders table that points to that endpoint.

Here’s a minimal example using the built-in FPDF library (lightweight but basic formatting):

// Add "Download Invoice" button next to View
add_filter( 'woocommerce_my_account_my_orders_actions', 'custom_add_invoice_button', 10, 2 );
function custom_add_invoice_button( $actions, $order ) {
    $actions['invoice'] = array(
        'url'  => add_query_arg( array(
            'download_invoice' => $order->get_id(),
        ), home_url() ),
        'name' => __( 'Download Invoice', 'woocommerce' ),
    );
    return $actions;
}

// Catch invoice download request
add_action( 'init', 'custom_generate_invoice_pdf' );
function custom_generate_invoice_pdf() {
    if ( isset( $_GET['download_invoice'] ) ) {
        $order_id = intval( $_GET['download_invoice'] );
        $order    = wc_get_order( $order_id );

        if ( ! $order ) return;

        // Load FPDF (must be available in your theme/plugin folder)
        require_once get_stylesheet_directory() . '/fpdf/fpdf.php';

        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont( 'Arial', 'B', 16 );
        $pdf->Cell( 40, 10, 'Invoice for Order #' . $order->get_id() );

        $pdf->Ln(20);
        $pdf->SetFont( 'Arial', '', 12 );
        $pdf->Cell( 40, 10, 'Customer: ' . $order->get_formatted_billing_full_name() );
        $pdf->Ln(10);
        $pdf->Cell( 40, 10, 'Total: ' . $order->get_formatted_order_total() );

        // Output PDF
        $pdf->Output( 'D', 'invoice-' . $order->get_id() . '.pdf' );
        exit;
    }
}

Notes:

Cheers.

Reasons:
  • Blacklisted phrase (1): Cheers
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Jer Salam