Jump to content

Best way to generate a consecutive number


Jon
 Share

Recommended Posts

Hello,

I am wondering what would be the best way to generate a consecutive order number. I have template "order" which I would like a number for.

My thoughts would be to retrieve the last order number on page save. Would that be the best solution?

Link to comment
Share on other sites

Hello Macrura,

Thanks for that. I seem to be having a problem getting the highest number. I thought about using ->last() to get the last page ID for the template which would be the last order in theory. But for some reasons I cant pick it up in ready.php worked within a page template. The code I using is 

$this->pages->addHookAfter('saveReady', null, 'order_number');

function order_number($event) {
    $page = $event->arguments[0];
    if($page->template != 'booking') return;
    if(!isset($page->booking_orderno)) {
        $max = $wire->pages->find("template=booking")->last();
        $page->booking_orderno = $max + 1;
    }

}

Link to comment
Share on other sites

Thank you guys, used the following

$this->pages->addHookAfter('saveReady', null, 'order_number');

function order_number($event) {
    $page = $event->arguments[0];
    if($page->template != 'booking') return;
    if(!isset($page->booking_orderno)) {
        $max = wire('pages')->get("template=booking, sort=-id");
        $lastNumber = $max->booking_orderno +1;
        $page->booking_orderno = $lastNumber;
    }

}

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...