Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi Gaspar,
    You can have full control over the invoice number formatting by using a filter in your theme’s functions.php:

    add_filter( 'wpo_wcpdf_invoice_number', 'wpo_wcpdf_invoice_number', 10, 4 );
    function wpo_wcpdf_invoice_number( $invoice_number, $order_number, $order_id, $order_date ) {
    	$prefix = 'ABC';
    	$order_year = date_i18n( 'Y', strtotime( $order_date ) );
    	$order_month = date_i18n( 'm', strtotime( $order_date ) );
    	$order_day = date_i18n( 'd', strtotime( $order_date ) );
    	$order_date_full = date_i18n( 'Y-m-d', strtotime( $order_date ) );
    
    	$padding = 6; // number of digits - so 42 becomes 000042
    	$suffix = 'X';
    	$invoice_number = $prefix . $order_date_full . sprintf('%0'.$padding.'d', $invoice_number) . $suffix ;
    	return $invoice_number;
    }

    Thread Starter gaspar

    (@gaspar)

    Thanks for this!

    Next question regarding this topic: how can i remove “invoice-” or change its place in file name?

    Plugin Contributor Ewout

    (@pomegranate)

    See FAQ, this can also be done with a filter:

    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    	// remove 'invoice-'
    	$new_filename = str_replace('invoice-', '', $filename)
    
    	return $filename;
    }

    the possibilities are virtually endless with the supplied function arguments ($template type, $order_ids and $context).

    Good luck!

    Ewout

    Thread Starter gaspar

    (@gaspar)

    Sorry, i already forgot this part in FAQ.

    P.S. Just FYI your code should be modified to work in multi-language environments (your plugin supports this).

    Thanks again and keep up the good work you have done so far!

    Plugin Contributor Ewout

    (@pomegranate)

    no problem, I just wanted to keep it simple for you. Here’s a more complete, internationalized example to build from:

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Can you add [order_day] shortcode also?’ is closed to new replies.