Jump to content

Advice for a pricing field/hook


suntrop
 Share

Recommended Posts

Hello everybody ?

I am in need of some form of advice or ideas. I've got a site that displays products along with prices. Prices stored in net, but showed on the frontend and newsletter as its gross value. We decided to use net because there are three different gross values (different countries, calculated via external API) and it makes programming easier.

So, now, my client wishes to input the gross value into the CMS. That would cause a lot of changes, (it is error-prone) and makes some things more difficult at our end of the product. Despite I'd like to give my clients what they need, I was thinking to use hooks for that. So whenever something is put into the field "price" will get converted before storing in the DB and vice versa gets converted to be shown as gross in the field value. That way, I "just" have to change it at a single point: the hook for the CMS field.

What you think? Is this kind of more a hacky way and brings in the long run more problems than short benefits? I am always used to work with what I actually have and see :-)

Link to comment
Share on other sites

One solution could be RockCalculator so that the client can easily calculate net from gross:

So in austria they could enter 120/1,2 and would end up with the net value of 100. I really like to have that feature in some of my systems. Very simple yet very powerful.

Another option is to hook the inputfield and add another input and some JavaScript:

<?php
$wire->addHookAfter("Inputfield(name=title)::render", function(HookEvent $event) {
  $field = $event->object;
  $field->appendMarkup = '<input type="text" name="foo">
    <script>
    $(document).on("input", "input[name=title]", function(e) {
      let val = e.target.value;
      $("input[name=foo]").val(val);
    });
    </script>';
});

 

Link to comment
Share on other sites

I wonder if FieldtypeMeasurement might help? Only in alpha at the moment so you would need to test well. Create a conversion php file with the calculations (see the help). Data entry would then be net or whatever gross you want, with interactive conversion as required. See the readme for more details. Happy to help if you want to go down this route. Note that both this and @bernhard’s suggestion require a field change, whereas a hook (if it works) might not, so you would need to handle data migration. 

Link to comment
Share on other sites

Thanks guys! 

@bernhard That is quite nice, to add the second field by JS. I was thinking of adding it via PW's hook system and do the conversion in PHP. But I like your idea. 

Had to change some parts, because sometimes the field is inside a repeater field and the names are price_repeater1234. Perhaps it is just me, but I would have thought conditional hooks take this into account. I had to remove the Inputfield(name=price) and check from within the hook. BTW, there is a new str_starts_with() function in PHP 8 ? 

@MarkE Looks interesting! Like the idea a lot. This time I'll stick with the simpler help of JS. 

  • 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...