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

    (@pomegranate)

    Hi!
    You’re correct that it is much better to do this globally, but please remember that this only works if all orders properly get an invoice number. Add this to your themes functions.php.

    add_filter( 'woocommerce_order_number', 'woocommerce_invoice_order_number', 99, 2);
    function woocommerce_invoice_order_number( $order_number, $order ) {
    	global $wpo_wcpdf;
    	$invoice_number = $wpo_wcpdf->export->get_invoice_number( $order->id );
    
    	return $invoice_number;
    }

    Let me know if that helps!

    Ewout

    Thread Starter lawkwok

    (@lawkwok)

    I get a 502 bad gateway error when I add that code to functions.php.

    Plugin Contributor Ewout

    (@pomegranate)

    I’m sorry, there’s a nesting issue here: one of the arguments used to format the invoice number is the order number, which calls this filter, which calls the function for formatting the invoice number etc. infinitely.

    This is a bit more code but should do the trick:

    add_filter( 'woocommerce_order_number', 'woocommerce_invoice_order_number', 99, 2);
    function woocommerce_invoice_order_number( $order_number, $order ) {
    	global $wpo_wcpdf;
    	$invoice_number = get_post_meta( $order->id, '_wcpdf_invoice_number', true);
    
    	// get format settings
    	$template_settings = get_option('wpo_wcpdf_template_settings');
    
    	$formats['prefix'] = isset($template_settings['invoice_number_formatting_prefix'])?$template_settings['invoice_number_formatting_prefix']:'';
    	$formats['suffix'] = isset($template_settings['invoice_number_formatting_suffix'])?$template_settings['invoice_number_formatting_suffix']:'';
    	$formats['padding'] = isset($template_settings['invoice_number_formatting_padding'])?$template_settings['invoice_number_formatting_padding']:'';
    
    	// Replacements
    	$order_year = date_i18n( 'Y', strtotime( $order->order_date ) );
    	$order_month = date_i18n( 'm', strtotime( $order->order_date ) );
    
    	foreach ($formats as $key => $value) {
    		$value = str_replace('[order_year]', $order_year, $value);
    		$value = str_replace('[order_month]', $order_month, $value);
    		$formats[$key] = $value;
    	}
    
    	// Padding
    	if ( ctype_digit( (string)$formats['padding'] ) ) {
    		$invoice_number = sprintf('%0'.$formats['padding'].'d', $invoice_number);
    	}
    
    	$formatted_invoice_number = $formats['prefix'] . $invoice_number . $formats['suffix'] ;
    
    	return $formatted_invoice_number;
    }

    Let me know if that helps!

    Thread Starter lawkwok

    (@lawkwok)

    This is perfect! Thanks for the code snippet. It helps eliminate overriding templates just to change the order ID.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Globally change order ID’ is closed to new replies.