Custom PDF filenames

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

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;
}
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;
}
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;
}

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) && method_exists($document->order, 'get_order_number')) {
            $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;
}