Editing the filename | WordPress.org

[ad_1]

Plugin Contributor
Mohamad

(@mohamadntr)

Hi @adi1122,

The image you displayed is related to “Number Format,” which refers to the invoice number, not the file name.

Do you intend to change the file name or invoice number?

Best regards

Hi @mohamadntr , Since the plugin by default will display the invoice pdf file name in this format invoice+invoice number (invoice-04124-25), I want to add next year to the invoice number as 04124-25(first three is invoice id + current year +”-” current year+1) so it automatically updates the filename as this. I was able to customize at the frontend for PDF using the PHP date(‘y’)+1, but not sure, but not sure how to do it for filename.

also when my invoice id reaches 99, for the next order it should be 100? and after 999 to 1000 and so on.

  • This reply was modified 4 hours, 11 minutes ago by adi1122.

Plugin Contributor
Mohamad

(@mohamadntr)

@adi1122

It seems you only want to modify your file name to add the next year’s number. If so, I can write a code snippet for you to do that.

also when my invoice id reaches 99, for the next order it should be 100? and after 999 to 1000 and so on.

Yes, since invoice numbers increment by 1.

Plugin Contributor
Mohamad

(@mohamadntr)

@adi1122

Please insert the below code to your website:

/**
* PDF Invoices & Packing Slips for WooCommerce
* Modify the file name of invoice document
*
* @param string $filename
* @param string $document_type
* @param array $order_ids
*
* @return string
*/
function wpo_custom_filename( string $filename, string $document_type, array $order_ids ): string {
if ( 'invoice' !== $document_type ) {
return $filename;
}

$filename_parts = explode( '.', $filename );
$order = wc_get_order( reset( $order_ids ) );
$order_date = is_callable( array( $order, 'get_date_created' ) ) ? $order->get_date_created() : null;

if ( ! empty( $order_date ) ) {
$filename_parts[0] .= '-' . ( $order_date->date_i18n( 'y' ) + 1 );
}

return implode( '.', $filename_parts );
}

add_filter( 'wpo_wcpdf_filename', 'wpo_custom_filename', 10, 3 );

This snippet will add “-{next-year-two-digits}” to your invoice file name.

If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters.

Best regards

@mohamadntr , thanks a lot. I also want to add the next year in the suffix too, /[order_date=”y”]-next year

Plugin Contributor
Mohamad

(@mohamadntr)

Please add the snippet below:

/**
* PDF Invoices & Packing Slips for WooCommerce
* Add [order_year+1] placeholder for invoice number
*/
function wpo_invoice_number_custom_placeholder( $formatted_number, $document_number, $document, $order ): string {
if ( strpos( $formatted_number, '[order_year+1]' ) === false || 'invoice' !== $document->get_type() ) {
return $formatted_number;
}

$order_date = is_callable( array( $order, 'get_date_created' ) ) ? $order->get_date_created() : null;

if ( empty( $order_date ) && function_exists( 'wc_string_to_datetime' ) ) {
$order_date = wc_string_to_datetime( date_i18n( 'Y-m-d H:i:s' ) );
}

$order_year = ! empty( $order_date ) ? $order_date->date_i18n( 'y' ) + 1 : null;

return str_replace( '[order_year+1]', $order_year, $formatted_number );
}

add_filter( 'wpo_wcpdf_format_document_number', 'wpo_invoice_number_custom_placeholder', 10, 4 );

After that, use this placeholder: [order_year+1].

You might need to disable the previous snippet.

Best regards

Great it is working, Thanks @mohamadntr

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer