Custom PDF filenames

Professional Extension #

If you own a license of the Professional extension or our PDF Invoice extensions bundle, you can change filenames simply via the plugin settings. Go to WooCommerce > PDF Invoices > Documents > (document of your choice), where you’ll find a field to enter the format for your custom filenames.

You can use plain text and the same placeholders as in the address customization. Here are some of the most used:

  • {{order_number}}
  • {{order_date}}
  • {{document_number}} (will display the invoice number for invoices, etc.)
  • {{formatted_billing_full_name}}
  • {{billing_first_name}}
  • {{billing_last_name}}

Free version #

To change the filename of the PDF attachments / downloads in the free version of the plugin, you can use the wpo_wcpdf_filename filter. Read this if you haven’t used wordpress filters / edited functions.php before!

Example 1: Prepend shop name #

add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    // prepend your shopname to the file
    $new_filename = 'myshopname_' . $filename;
 
    return $new_filename;
}

Example 2: Replace ‘invoice’ with something else #

add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    $invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
    $new_prefix = _n( 'booking', 'bookings', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
    $new_filename = str_replace($invoice_string, $new_prefix, $filename);
 
    return $new_filename;
}

Example 3: Remove ‘invoice’ #

add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    $invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
    $new_filename = str_replace($invoice_string.'-', '', $filename);
 
    return $new_filename;
}

Example 4: Change all filenames #

This example doesn’t actually change anything as is, but you can use it as a basis to fully customize the filenames for all documents.

add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    $count = count($order_ids);
 
    switch ($template_type) {
        case 'invoice':
            $name = _n( 'invoice', 'invoices', $count, 'woocommerce-pdf-invoices-packing-slips' );
            break;
        case 'packing-slip':
            $name = _n( 'packing-slip', 'packing-slips', $count, 'woocommerce-pdf-invoices-packing-slips' );
            break;
        case 'proforma':
            $name = _n( 'proforma-invoice', 'proforma-invoices', $count, 'wpo_wcpdf_pro' );
            break;    
        case 'credit-note':
            $name = _n( 'credit-note', 'credit-notes', $count, 'wpo_wcpdf_pro' );
            break;
        default:
            $name = $template_type;
            break;
    }
 
    if ( $count == 1 ) {
        $document = wcpdf_get_document( $template_type, $order_ids );
        if ( $number = $document->get_number() ) {
            $suffix = $number;
        } elseif ( isset($document->order) ) {
            $number = $document->order->get_order_number();
        } else {
            $number = $order_ids[0];
        }
        $suffix = $number;
    } else {
        $suffix = date('Y-m-d'); // 2020-11-11
    }
  
    $filename = $name . '-' . $suffix . '.pdf';
 
    return $filename;
}