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), 'wpo_wcpdf' );
	$new_prefix = _n( 'booking', 'bookings', count($order_ids), 'wpo_wcpdf' );
	$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), 'wpo_wcpdf' );
	$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 ) {
	global $wpo_wcpdf, $wpo_wcpdf_pro;
	$count = count($order_ids);

	switch ($template_type) {
		case 'invoice':
			$name = _n( 'invoice', 'invoices', $count, 'wpo_wcpdf' );
			$number = $wpo_wcpdf->export->get_display_number( $order_ids[0] );
			break;
		case 'packing-slip':
			$name = _n( 'packing-slip', 'packing-slips', $count, 'wpo_wcpdf' );
			$number = $wpo_wcpdf->export->order->get_order_number();
			break;
		case 'proforma':
			$name = _n( 'proforma-invoice', 'proforma-invoices', $count, 'wpo_wcpdf_pro' );
			$number = $wpo_wcpdf_pro->get_number('proforma');
			break;		
		case 'credit-note':
			$name = _n( 'credit-note', 'credit-notes', $count, 'wpo_wcpdf_pro' );
			$number = $wpo_wcpdf_pro->get_number('credit-note');
			break;
		default:
			$name = $template_type;
			$number = $wpo_wcpdf->export->order->get_order_number();
			break;
	}

	if ( $count == 1 ) {
		$suffix = $number;
	} else {
		$suffix = date('Y-m-d'); // 2020-11-11
	}

	$filename = $name . '-' . $suffix . '.pdf';

	return $filename;
}