• Resolved cherry6688

    (@cherry6688)


    Hello,

    We would like to have invoice number reset by daily and using below code snippet, but it seems that the order date seems to get post_date_gmt date instead of post_date? the problem is the invoice number duplicated with same running number 001 when local time (current date) is over 12am something on 27 Aug but GMT time is still 26 Aug, it will always start with ‘1’, kindly advise how can we fix this? thanks.

    add_action( 'wpo_wcpdf_before_sequential_number_increment', 'wpo_wcpdf_reset_invoice_number_daily', 10, 3 );
    function wpo_wcpdf_reset_invoice_number_daily( $number_store, $order_id, $date ) {
    	if ( $number_store->store_name == 'invoice_number' ) {
    		$current_day = date('z');
    		$last_number_day = $number_store->get_last_date('z');
    		// check if we need to reset
    		if ( $current_day != $last_number_day ) {
    			$number_store->set_next( 1 );
    		}
    	}
    }
    • This topic was modified 3 years, 8 months ago by cherry6688.
    • This topic was modified 3 years, 8 months ago by cherry6688.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Darren Peyou

    (@dpeyou)

    Hello @cherry6688,

    Does this mean that your actual problem is with time? Maybe we should be thinking about using time, but with an offset in the $current_day variable?

    Right now you are using date( 'z' ) where z: The day of the year (from 0 through 365). I think you’ll need to change that to include hours & set the offset with math operations on the hours.

    So I think you’ll need to use G: for hours & i: for minutes

    Thread Starter cherry6688

    (@cherry6688)

    Hello.. Yes I think the problem should be the timezone? For example when I create invoice on local timezone 12.15am 27 Aug (Universal time is 26 Aug 4.15pm) I get invoice number like I220827001, then I create again new invoice I will still get I220827001 which is duplicated invoice number, if I create invoice at 8am 27 Aug and Universal time is 27 Aug 12am then I will get correct invoice number which is I220827002.
    How can I minus hours from current day?
    Thank you very much.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @cherry6688,

    It seems that the issue is in this line, that displays the current day number, based on the universal time (UTC+0):
    $current_day = date('z');

    …but you should calculate it using the WordPress local time as reference instead, that is your site’s timezone, replacing that line with this one:
    $current_day = wp_date('z');

    Please replace your code with this one:

    add_action( 'wpo_wcpdf_before_sequential_number_increment', 'wpo_wcpdf_reset_invoice_number_daily', 10, 3 );
    function wpo_wcpdf_reset_invoice_number_daily( $number_store, $order_id, $date ) {
    	if ( $number_store->store_name == 'invoice_number' ) {
    		$current_day = wp_date('z');
    		$last_number_day = $number_store->get_last_date('z');
    		// check if we need to reset
    		if ( $current_day != $last_number_day ) {
    			$number_store->set_next( 1 );
    		}
    	}
    }

    Let us know if it worked!

    Thread Starter cherry6688

    (@cherry6688)

    Hi @yordansoares

    It works with wp_date, thank you very much and appreciate your help.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I’m glad to hear that it worked, @cherry6688!

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Reset invoice number by daily’ is closed to new replies.