Jump to content

Recommended Posts

Posted

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?

Posted

sure, you could have a hook on page save ready, which checks to see if page has an order number and if not it gets the order with the highest number and adds 1, and then sets the field value

  • Like 1
Posted

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

}

Posted (edited)

$max = wire('pages')->get("template=booking, sort=id");

$lastNumber = $max->your_number_field + 1;

Edited by LostKobrakai
added a code-block for you :)
  • Like 1
Posted

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

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
  • Recently Browsing   0 members

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