• Resolved anagam2016

    (@anagam2016)


    Hi Ewot,

    You had helped me earlier with customization of pdf file name.
    Need some more help.
    I am using the following code now to get pdf invoice in format Y-m-d-Invoice-No.pdf

    /** PDF Invoice File name - Replace ‘invoice’ with 'Фактура' */
    
    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' ); 
    
        if ( $order_ids == 1 ) {
            $suffix = $number;
        } else {
            $suffix = date('Y-m-d'); // 2020-11-11
        }
       
        $new_prefix = _n( $suffix . '-' . 'Фактура', $suffix . '-' . '-Фактури', count($order_ids), 'wpo_wcpdf' );
        $new_filename = str_replace($invoice_string, $new_prefix, $filename);
     
        return $new_filename;
    }

    It works for current date. Please help me how to get the file name with the invoice date.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter anagam2016

    (@anagam2016)

    Thanking you in advance!

    Plugin Contributor Ewout

    (@pomegranate)

    Hi!
    date() indeed gets the current date. You can get the invoice date from the order with:

    
    $order_id = array_shift($order_ids);
    $invoice_date = get_post_meta($order_id,'_wcpdf_invoice_date',true);
    $invoice_date = date_i18n( 'Y-m-d', strtotime($invoice_date) );
    

    or integrated into your filter:

    
    /** PDF Invoice File name - Replace ‘invoice’ with 'Фактура' */
    
    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' ); 
    
        if ( $order_ids == 1 ) {
            $order_id = array_shift($order_ids);
            $invoice_date = get_post_meta($order_id,'_wcpdf_invoice_date',true);
            $invoice_date = date_i18n( 'Y-m-d', strtotime($invoice_date) );
            $suffix = $number.'-'.$invoice_date;
        } else {
            $suffix = date('Y-m-d'); // 2020-11-11
        }
       
        $new_prefix = _n( $suffix . '-' . 'Фактура', $suffix . '-' . '-Фактури', count($order_ids), 'wpo_wcpdf' );
        $new_filename = str_replace($invoice_string, $new_prefix, $filename);
     
        return $new_filename;
    }
    

    (note that this only works for single exports)

    Anyone else reading this, check the documentation for more examples: Custom PDF filenames.

    Hope that helps!
    Ewout

    Plugin Contributor Ewout

    (@pomegranate)

    Noticed an error in your code:

    
        if ($order_ids == 1) { 
    

    should be:

    
        if (count($order_ids) == 1) { 
    
    Thread Starter anagam2016

    (@anagam2016)

    Hi!

    Thanks a lot!
    It works perfectly.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Pdf file name customization’ is closed to new replies.