Jon Posted January 28, 2016 Posted January 28, 2016 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?
Macrura Posted January 28, 2016 Posted January 28, 2016 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 1
Jon Posted January 28, 2016 Author Posted January 28, 2016 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; } }
Martijn Geerts Posted January 28, 2016 Posted January 28, 2016 (edited) $max = wire('pages')->get("template=booking, sort=id"); $lastNumber = $max->your_number_field + 1; Edited January 28, 2016 by LostKobrakai added a code-block for you :) 1
Jon Posted January 28, 2016 Author Posted January 28, 2016 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; } } 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now