douglas81 Posted July 11, 2016 Share Posted July 11, 2016 Hey all! How can I do this... I have a product template. The product template has a price field, which is for prices ex. VAT. (I have several hundred products already populated with these ex. VAT prices.) I have added another price field which is for prices including VAT. In the first circumstance, I'm trying to work out a way to iterate though each product page and auto-populate the inc. VAT price field. After that, I need a way of doing the following... If I edit/populate the ex. VAT price field, I would like the inc. VAT price field to auto-populate. Similarly, if I choose to edit the inc. VAT price field, on page (or field save) I would like the ex. VAT price field to update. I'm doing so many different wee things for this site at the moment that I'm losing focus on this little issue. Any advice on how best to achieve this would be much appreciated! Link to comment Share on other sites More sharing options...
horst Posted July 11, 2016 Share Posted July 11, 2016 You need a bootstrap script that should be located besides the PWs index.php. I have a template for that: Spoiler <?php $productsSelector = 'template=product'; // define your selector here // bootstrap PW and check user -------------------------------------------- include(dirname(__FILE__) . '/index.php'); $user = wire('user'); if(!$user->isSuperuser()) { exit(); } // ------------------------------------------------------------------------ // set time limit --------------------------------------------------------- #ignore_user_abort(true); // true | false set_time_limit(300); // ------------------------------------------------------------------------ // prepare server for plaintext output ------------------------------------ if(function_exists('apache_setenv')) @apache_setenv('no-gzip', '1'); @ini_set('zlib.output_compression', 'Off'); @ini_set('output_buffering ', '0'); @ini_set('implicit_flush', '1'); @ob_implicit_flush(true); @ob_end_flush(); if(isset($_SERVER['HTTP_HOST'])) { header('Content-Type: text/plain'); } // ------------------------------------------------------------------------ $pages = wire('pages'); $productpages = $pages->find($productsSelector); $max = count($productpages); $cur = 0; foreach($productpages as $p) { set_time_limit(30); $cur++; echo "|----[$cur/$max]--{$p->id}--{$p->title}\n"; // now do your calculation / population and save the field / page /// ... $p->save(); $pages->uncache($p); echo "|\n"; break; // comment this out if it all works as expected! } echo "\nREADY!\n"; After you have setup all your products, I would write a hook into the site/ready.php. Hook into before pages save, check if it is a productpage, if so, calculate the incl. vat price and populate it to the field, Ready! Spoiler $wire->addHookAfter('Pages::saveReady', function(HookEvent $event) { $p = $event->arguments('page'); if('product' != $p->template) return; if($p->productSkipMe) return; // now do the calculation / population $p->inclvat = ($p->exclvat / 100) * (100 + $vatpercent); $p->productSkipMe = true; // suppress further processing }); 6 Link to comment Share on other sites More sharing options...
Robin S Posted July 11, 2016 Share Posted July 11, 2016 Rather than having two price fields... price price_including_vat ...have you considered having one price field and one VAT rate (%) field? price vat_rate And then you calculate the VAT inclusive price on-the-fly. Seems like it would be easier to maintain this way. 4 Link to comment Share on other sites More sharing options...
horst Posted July 11, 2016 Share Posted July 11, 2016 23 minutes ago, Robin S said: And then you calculate the VAT inclusive price on-the-fly. Seems like it would be easier to maintain this way. This depends on if there is only one VAT %. In germany there are two, or three: 0% 7% 19% If there are multiple VAT % possible, I think it is better to maintain with the saveReady hook, and if one day the VAT will adjusted to another % value, run the bootstrap script again. But if there is only one VAT %, @Robin S solution seems to be better. If this is the case, I would add a property hook to $page, that gives me the VAT-include calculated value. (I think, this is what Robin have in mind too.) EDIT: Ah, totally silly. Robins solution is better, regardless if you have one or multiple VAT % values. 1 Link to comment Share on other sites More sharing options...
Robin S Posted July 11, 2016 Share Posted July 11, 2016 I'm no expert in this, but the way I see it is you have a base price and then one or more 'modifiers' that act on the base price. A modifier could be a tax rate, an export tariff, an addon handling fee for a large item, or something else. You don't work out the results of these modifiers for each base price ahead of time, you just apply them using PHP math operations as needed depending on what options the website visitor has selected. I think the performance impact of basic math operations like this would be negligible. 4 Link to comment Share on other sites More sharing options...
adrian Posted July 11, 2016 Share Posted July 11, 2016 From my experience, @Robin S's solution is definitely the way to go! 3 Link to comment Share on other sites More sharing options...
douglas81 Posted July 12, 2016 Author Share Posted July 12, 2016 Thanks for all the suggestions. Yes, perhaps I'm over complicating things. Basically, I'm trying to make updating and maintaining a large product price list as simple as possible. Most often manufacturers will provide us with a price list which is inc. VAT. I need to be able to quickly scan through a list of product pages and check those prices. We ship outside of the EU also, so the price field I have at the moment is ex. VAT. I have a VAT field also. For simplicity of the site admin, I have a bookmark in page lister which shows all products (i.e. template=product). In the resulting list, as well as displaying the ex.VAT field, I would like a field that shows the price inc. VAT. This doesn't need to be editable, and it cou;d be generated on the fly using PHP, but I don't know how or where to hook that in. (I originally thought it may be nice to be able to modify the inc.VAT price and have the ex.VAT price update automatically, but that's unnecessary for now.) In terms of template rendering (PHP) for the site visitor, I'm always using the ex. VAT price and generating inc. VAT prices using the ex.VAT price * tax field, depending on the product type and the customer's location. Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 12, 2016 Share Posted July 12, 2016 23 minutes ago, douglas81 said: and it could be generated on the fly using PHP, but I don't know how or where to hook that in. http://modules.processwire.com/modules/fieldtype-runtime-markup/ 4 Link to comment Share on other sites More sharing options...
Robin S Posted July 12, 2016 Share Posted July 12, 2016 1 hour ago, douglas81 said: For simplicity of the site admin, I have a bookmark in page lister which shows all products (i.e. template=product). In the resulting list, as well as displaying the ex.VAT field, I would like a field that shows the price inc. VAT. Besides using the Fieldtype module @LostKobrakai suggested (I haven't used it myself), you could create a simple Process module to list pages with the product template alongside the calculated VAT inclusive price. Check out the example module code shared by @renobird here. Edit: I just installed RuntimeMarkup and it works great for math calculations. So using this module would be the way to go if you want to see calculated values in a Lister. 2 Link to comment Share on other sites More sharing options...
douglas81 Posted July 12, 2016 Author Share Posted July 12, 2016 Yep, FieldTypeRuntime module is the answer, at least for the short term. Works perfectly! Thanks everyone!!! 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