Peter Falkenberg Brown Posted July 6, 2017 Share Posted July 6, 2017 Hi All, I might have missed something, but I couldn't find this... I'm looking for a field type that will calculate a result based on numeric values in other fields in the same page, and store the data when the page is saved, and update it when the values change. (Rather than just displaying the values dynamically at run time, although I suppose that would be better than nothing.) I.e. "order_total" = "quantity" * "unit_price." with order_total being the calculating field type. Of course, I could write template php files that do this, but I'm trying to wrestle with the 3.x admin pages to essentially record data for an ordering system that will only be used by internal staff, so that I don't have to design a complete front-end for the system. I've got the customer field set, and the items, and the sales (i.e. "orders") pages set up. I'm trying out the repeater field to handle multiple items in one order, i.e. 4 copies of this, 10 copies of that, all pulled with page select fields from the items "table." (Rather than create one page for each "order item".) Most orders won't have more than 1 or 2 items, and I think they would max out at less than 10 order items. A native "calc" field type would help tremendously, or an admin spot to tell a float field to run a php file when saving or updating the page, that would do the calculations, would also work. Thanks! Peter Link to comment Share on other sites More sharing options...
Robin S Posted July 6, 2017 Share Posted July 6, 2017 You can create a new integer field, add it to your template, and update the value based on other values in the page with a hook to Pages::saveReady. No need for a new fieldtype I think because it would have to use a hook like this anyway, because by themselves fields don't know anything about other fields that might be in the same template. 2 Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted July 6, 2017 Author Share Posted July 6, 2017 Hi Robin, Although I've built large front end apps with PW's API, I've never tried to modify the admin back end. Based on the interface that it has, I'm under the impression that one would have to modify the files under wire/templates-admin, or make a copy of the admin template into the site folder and modify code there. Do you have any tips on the best way to achieve your idea above? Are there any examples of this type of Pages::saveReady call that you know of? Thanks! Peter Link to comment Share on other sites More sharing options...
Robin S Posted July 6, 2017 Share Posted July 6, 2017 23 minutes ago, Peter Falkenberg Brown said: Do you have any tips on the best way to achieve your idea above? Are there any examples of this type of Pages::saveReady call that you know of? Here is an example - change the template name to suit. Assumes three integer fields: quantity, unit_price, order_total. In /site/ready.php... $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name !== 'your_template_name') return; if($page->quantity && $page->unit_price) { $page->order_total = $page->quantity * $page->unit_price; } }); 3 Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted July 6, 2017 Author Share Posted July 6, 2017 Hi Robin, Thanks... that code is clear and understandable. I think I'm a bit stuck on the fact that I'm not using template files with this app --- I'm only accessing the data from the admin pages, so unless I'm missing something, I would think that the code above would have to be incorporated into one of the admin template files. Is that correct, and if so, any thoughts on which one? One of the downsides of copying out the admin template files to the site directory, in my opinion, is that the default admin template files get overwritten on certain updates. Kind of messing with the core, in my opinion. I think it would be really cool if PW had an add-on module which could define a calculated field like this. Or even make it a special "calc" field type. Yours, Peter Link to comment Share on other sites More sharing options...
BitPoet Posted July 6, 2017 Share Posted July 6, 2017 You don't need to touch any core files, just create a file named "ready.php" in the site folder and put the code there. You can read up on the files for site hooks here. 2 Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted July 6, 2017 Author Share Posted July 6, 2017 Dear Robin and BitPoet, Wow!! This is why I love ProcessWire. The details above are exactly what I need. And of course it was already there --- I just hadn't kept up to date with all of the new documentation, during the last couple of years when I was focused on other projects besides PW app development. So... gotta catch up. Thanks to you both! Peter 1 Link to comment Share on other sites More sharing options...
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