Setting a form's currency based on user's selection

By: Henry King | Asked: 08/11/2023
ForumsCategory: General questionsSetting a form's currency based on user's selection
Henry King asked 8 months ago
Hi
I would like to set the form's currency based on user's selection of the currency field in the form. E.g. there is a dropdown list where the user can select among a list of currencies such as USD, AUD, GBP, JPY etc. How do I customize the function below to achieve this or can it be done through other methods? Thanks for help! frm_currency - Formidable Forms add_filter( 'frm_currency', 'frm_custom_currency', 10, 2 ); function frm_custom_currency( $currency, $form ) { $form_id = is_object( $form ) ? $form->id : $form; if ( $form_id == 50 ) { // Change 50 to your form ID $currency = FrmProCurrencyHelper::get_currencies( 'AUD' ); // Change AUD to the three letter code or include your currency array here } return $currency; }
1 Answers
Victor Font answered 8 months ago
You asked the same question a week ago in this thread: https://connect.formidableforms.com/question/category/how-to/changing-currency-in-form-based-on-user-selection-2/. In the last post, you said everything was working. What happened to the working solution?
Henry King replied 8 months ago

<p>Hi, I said the solution is working to the wrong question and I just realized the mistake. I still can't figure out to get the second solution to work. My question this time is slightly different from earlier as I would like to use Formidable Forms frm_currency filter rather than use external libraries / javascript. Wonder if that is possible?</p>

Victor Font replied 8 months ago

I'm under the impression that you may be new to WordPress Hooks. Please allow me to explain how filters work. Filters are PHP snippets that allow developers to use the WordPress Hooks API to change content and data either before or after a form paints in the browser. In general, they should not be used to support user interactions in a real time browser environment, unless there's no other way to resolve a specific design concept without making round trip data requests to the server.

Filters run on the server only while the form is being readied to be sent to an end user's browser OR after the submit button is pressed and the after submit entry management or navigation actions take place. After the form is painted in the browser, the filter is no longer available to you until the form is submitted or the browser is refreshed.

You asked if using the filter is possible. Yes, it's possible, but only if you use Ajax. Since the filter resides on the server only, each time a user changes the desired currency on the form entry, you must have a custom jQuery function to monitor the change and send the HTTP REQUEST to your server where your custom PHP callback function processes the information and in turn, transmits the RESPONSE back to the server to complete the request.

Creating the wrapper code to use a filter through Ajax to suit your requirement can only be solved through a custom programming solution and, unfortunately, it will not offer any value to a UX requirement that requires instant feedback to the end user. This is why I first suggested that you use the built-in browser library. It's ubiquitous and simple to implement. It still requires custom jQuery code, but with jQuery, you can find many examples on the web for what you want to do that are adaptable to Formidable Forms.

Henry King replied 8 months ago

Hi
Thanks. You are right. Since filter is a server side script, it is not ideal to solve my problem.
Let me try out your earlier solution and see if I can implement them properly.

Cheers.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right