Autofill Query
-
Hi there,
I have a query with regards to the autofill feature for fields.
Is is possible to autofill fields with user data from WooCommerce such as: billing_company, billing_address, billing_city etc.?
As of now I’ve only been able to make use of the basic WordPress user data.
Any help with this would be appreciated.
Thank you.
-
Hi @aidba55
I hope you are doing well.
I am afraid it is not possible out of the box, but Forminator has some hooks that can help with this.
Could you please let us know if you are using a regular text field?
Best Regards
Patrick FreitasHi Patrick,
Thanks for getting back to me.
That’s correct – I am making use of regular text fields.
The full list of WooCommerce data I’d like to have the ability to autofill:
billing_company
billing_address_1
billing_address_2
billing_city
billing_postcode
billing_country
billing_state
billing_email
billing_phoneThank you.
Hi @aidba55
Thank you for response!
As far as I’m aware, that data is stored as standard user meta data so please try following solution.
1. Create an empty file with a .php extension (e.g. “forminator-woo-autofill.php”)
2. copy and paste following code into it:<?php add_action( 'forminator_before_form_render', 'wpmudev_generate_acf_field_data', 10, 5 ); function wpmudev_generate_acf_field_data( $id, $form_type, $post_id, $form_fields, $form_settings ){ if( $id == 2094 ){ add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled', 10, 2 ); } } function wpmudev_get_acf_field_prefilled( $html, $field ){ if( !is_user_logged_in() ){ return $html; } $user_id = get_current_user_id(); $slug = $field['element_id']; $map_fields = array( 'text-4' => 'text-3', 'text-5' => 'text-7', 'text-6' => 'text-8', 'text-7'=> 'text-6', 'text-8' => 'phone-1' ); foreach( $map_fields as $key => $value ){ if( $slug == $key ){ if( strpos( $html, 'value=""' ) !== false ){ $default_val = get_user_meta( $user_id, $value, true ); if( $default_val ){ $html = str_replace( 'value=""', "value=$default_val", $html ); } } } } return $html; }
3. Make adjustments to the code like this:
a) in this line
if( $id == 2094 ){
replace the number with ID of your form; it should be the same number as in form shortcode
b) and in this part
$map_fields = array( 'text-4' => 'text-3', 'text-5' => 'text-7', 'text-6' => 'text-8', 'text-7'=> 'text-6', 'text-8' => 'phone-1' );
you would “map” meta data to form fields. Each “pair” like
'text-4' => 'text-3'
is one “mapping” (or auto-fill), where the first ID is the from field ID and the second is they user meta field key. For example, let’s say that you want to autofill field text-5 on the form with user billing city. The billing city in meta data in database is “billing_city” so this would become
'text-5' => 'billing_city'
I hope that makes some sense to you.
4. Once you got that all set, save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.
It should then work out of the box.
Best regards,
AdamHey Adam,
Thank you very much for the time and effort put into this custom code.
I have spent some time incorporating this code and it’s instructions. I am unable to get the autofill to work at this point in time. Currently the text field still sits empty.
I do have some experience with Must-use plugins and can confirm that I have followed these instructions to the letter and ensured that all IDs and meta data are correctly setup. I have also made sure that the logged-in test user has these billing details already stored to ensure it works.
Just to confirm, under the form’s “Behavior” and “Autofill” settings – the text fields in question have “Disable Autofill” as they’re default setting (not sure if this has any role to play in the autofill not working).
Any further help would be appreciated.
Thanks
Hi @aidba55
Thanks for response!
I just tested this code on my own end and it seems to work fine. I suppose there’s either some issue with how the code is implemented on your site or there’s some additional aspect involved that we are not yet aware of.
To start with, can you share the code exactly as you added it so I could confirm it’s fine?
When sharing, please make sure that it’s either marked as a code using “Code” tool of this post editor or use “pastebin.com” service instead (and just share a link to paste) in response.
It would also be helpful if you could share the link to the page in question – to which the code is applied.
Best regards,
AdamHi Adam,
I tracked the issue down to a bit of unrelated custom code that was causing a conflict.
I can confirm all is working 100% now.
Thanks again for your time and effort with this!
Regards.
Hi Adam,
I’ve just picked up one small bug. The autofill appears to only display one word.
For example:billing_company: Mike’s Autoworks
Autofill: Mike’sOnly the first word appears in the field.
Regards.
Hi @aidba55,
Could you please share the code so we can look closely at this?
Please use this post editor’s “Code” tool or the “pastebin.com” service instead, as we have already mentioned.
We look forward to hearing back from you.
Kind Regards,
Nebu John<?php add_action( 'forminator_before_form_render', 'wpmudev_generate_acf_field_data', 10, 5 ); function wpmudev_generate_acf_field_data( $id, $form_type, $post_id, $form_fields, $form_settings ){ if( $id == 114 ){ add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled', 10, 2 ); } } function wpmudev_get_acf_field_prefilled( $html, $field ){ if( !is_user_logged_in() ){ return $html; } $user_id = get_current_user_id(); $slug = $field['element_id']; $map_fields = array( 'text-7' => 'billing_city', 'text-8' => 'billing_state', 'text-9' => 'billing_postcode', 'text-10'=> 'billing_country', 'text-2' => 'billing_company', 'text-6' => 'billing_address_1', 'text-11' => 'billing_address_2' ); foreach( $map_fields as $key => $value ){ if( $slug == $key ){ if( strpos( $html, 'value=""' ) !== false ){ $default_val = get_user_meta( $user_id, $value, true ); if( $default_val ){ $html = str_replace( 'value=""', "value=$default_val", $html ); } } } } return $html; }
Hi @aidba55
I hope you are doing well.
I could replicate the issue, can you update the $html line using this?
Replace
$html = str_replace( 'value=""', "value=$default_val", $html );
to
$html = str_replace( 'value=""', "value='$default_val'", $html );
Best Regards
Patrick FreitasThanks Patrick,
Perfect, I can confirm it is working now. There is, however, still one small issue with regards to apostrophes in the field.
The apostrophe causes the autofill to abruptly stop:
e.g.
billing_company: Mike’s Autoworks
Autofill result: Mike<?php add_action( 'forminator_before_form_render', 'wpmudev_generate_acf_field_data', 10, 5 ); function wpmudev_generate_acf_field_data( $id, $form_type, $post_id, $form_fields, $form_settings ){ if( $id == 114 ){ add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled', 10, 2 ); } } function wpmudev_get_acf_field_prefilled( $html, $field ){ if( !is_user_logged_in() ){ return $html; } $user_id = get_current_user_id(); $slug = $field['element_id']; $map_fields = array( 'text-7' => 'billing_city', 'text-8' => 'billing_state', 'text-9' => 'billing_postcode', 'text-10'=> 'billing_country', 'text-2' => 'billing_company', 'text-6' => 'billing_address_1', 'text-11' => 'billing_address_2' ); foreach( $map_fields as $key => $value ){ if( $slug == $key ){ if( strpos( $html, 'value=""' ) !== false ){ $default_val = get_user_meta( $user_id, $value, true ); if( $default_val ){ $html = str_replace( 'value=""', "value='$default_val'", $html ); } } } } return $html; }
Hi @aidba55
Instead of
"value='$default_val'"
can you try
'value=\"$default_val\"'
but it’s recommended to test on staging first.Kind Regards,
KrisHi Kris,
Great, thanks.
I have tried the latest code. It results in the text\”$default_val\” autofilling the fields unfortunately.
I’ve pasted my code below just to ensure it’s correct.
<?php add_action( 'forminator_before_form_render', 'wpmudev_generate_acf_field_data', 10, 5 ); function wpmudev_generate_acf_field_data( $id, $form_type, $post_id, $form_fields, $form_settings ){ if( $id == 114 ){ add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled', 10, 2 ); } } function wpmudev_get_acf_field_prefilled( $html, $field ){ if( !is_user_logged_in() ){ return $html; } $user_id = get_current_user_id(); $slug = $field['element_id']; $map_fields = array( 'text-7' => 'billing_city', 'text-8' => 'billing_state', 'text-9' => 'billing_postcode', 'text-10'=> 'billing_country', 'text-2' => 'billing_company', 'text-6' => 'billing_address_1', 'text-11' => 'billing_address_2' ); foreach( $map_fields as $key => $value ){ if( $slug == $key ){ if( strpos( $html, 'value=""' ) !== false ){ $default_val = get_user_meta( $user_id, $value, true ); if( $default_val ){ $html = str_replace( 'value=""', 'value=\"$default_val\"', $html ); } } } } return $html; }
Regards.
Hi @aidba55
I pinged our SLS Team to review that closer. Thank you for your patience while we look into this further.
Kind Regards,
KrisHello @aidba55 ,
I’m sorry for the late response. Please try
"value=$default_val"
instead of'value=\"$default_val\"'
.kind regards,
Kasia
- The topic ‘Autofill Query’ is closed to new replies.