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

    (@pomegranate)

    Hi there,
    You can do this by filtering the invoice number. Place this code in your theme’s functions.php:

    add_filter( 'wpo_wcpdf_invoice_number', 'wpo_wcpdf_invoice_number', 999, 4 );
    function wpo_wcpdf_invoice_number( $invoice_number, $order_number, $order_id, $order_date ) {
    	$template_settings = get_option('wpo_wcpdf_template_settings');
    
    	// get format 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_date ) );
    	$order_month = date_i18n( 'm', strtotime( $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;
    	}
    
    	// check if prefix/suffix in DB
    	$invoice_prefix = get_post_meta( $order_id, '_wcpdf_invoice_prefix', true );
    	$invoice_suffix = get_post_meta( $order_id, '_wcpdf_invoice_suffix', true );
    	if (empty($invoice_prefix)) {
    		//store prefix/suffix
    		update_post_meta( $order_id, '_wcpdf_invoice_prefix', $formats['prefix'] );
    		update_post_meta( $order_id, '_wcpdf_invoice_suffix', $formats['suffix'] );
    	} else {
    		//use prefix/suffix from db
    		$formats['prefix'] = $invoice_prefix;
    		$formats['suffix'] = $invoice_suffix;
    	}
    
    	// 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;
    }

    Ewout

    Thread Starter Max Terbeck

    (@targetimc)

    Thanks, this filter works perfectly!
    By the way we’ve bought your plugin WooCommerce Print Address Labels https://wpovernight.com/downloads/woocommerce-print-address-labels/
    and it’s also great!

    Plugin Contributor Ewout

    (@pomegranate)

    That’s great and thanks for the compliment!

    Happy selling!
    Ewout

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Prefix & Invoice Number in DB’ is closed to new replies.