Foros de Roadsters.es

Avisos
Vaciar todo

Mejoras en Forminator. Carga de metadatos en formulario.

Papaventux
(@papaventux)
Respuestas: 1632
Miembro Admin
Topic starter
 

Nos dicen esto. En lectura superficial parece fácil de implementar.

I hope you’re well today!

Currently Forminator can only use limited/certain user meta for autofill function but you should be able to expand it to use your ACF data with a bit of custom code.

You can try this code:

$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;
}
To apply it to your site:

1. create an empty file with a .php extension (e.g. “formiantor-acf-user-fill.php”)
2. copy and paste code into it
3. do some small adjustments

a) in this line

if( $id == 2094 ){

replace the number with an ID of your form (form ID is the number that you can see in form’s 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.

3. finally, save the file and upload it to the “/wp-content/mu-plugins” of your site’s WP installation.

It should work out of the box then.

Best regards,
Adam

 
Respondido : 01/03/2023 12:44 pm
Papaventux
(@papaventux)
Respuestas: 1632
Miembro Admin
Topic starter
 

De momento funcionando parcialmente (Los campos de dirección no los carga)
El código:


<?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 == 256 ){ /* Formulario Ropa Roadsters III */
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' => 'DNI',  // DNI en CIF
                            'text-24' => 'Telefono',// Telefono
                            'address-1-street_address' => 'Direccion',
//<p>{address-1-city} {address-1-state} {address-1-zip} {address-1-country}
                            'address-1-zip' => 'CodigoPostal'
                           
                            //'address-1' => 'text-8', //dirección
                            );
    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;
}
 
Respondido : 03/03/2023 12:50 am
Papaventux
(@papaventux)
Respuestas: 1632
Miembro Admin
Topic starter
 

Notas!!!!

No me funciona con los campos específicos de telefono (si con los textos) y tampoco con los de "dirección " que son compuestos.

 
Respondido : 03/03/2023 1:03 am
Papaventux
(@papaventux)
Respuestas: 1632
Miembro Admin
Topic starter
 

Funcionando el formulario del Sierra del Segura también. (Los dos simultáneamente)


<?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 == 256 ){ /* Formulario Ropa Roadsters III */
add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled_ropa_roadstera', 10, 2 );
}
if( $id == 211 ){ /* Formulario Sierra Segura [forminator_form id="211"] */
add_filter( 'forminator_field_text_markup', 'wpmudev_get_acf_field_prefilled_sierra_segura', 10, 2 );
}
}
function wpmudev_get_acf_field_prefilled_ropa_roadstera( $html, $field ){
if( !is_user_logged_in() ){
return $html;
}
$user_id = get_current_user_id();
$slug = $field['element_id'];
    $map_fields = array(
                            'text-4' => 'DNI',  // DNI en CIF
                            'text-24' => 'Telefono',// Telefono
                            'address-1-street_address' => 'Direccion', //Don't work
                            'address-1-zip' => 'CodigoPostal' //Don't work
                            //{address-1-city} {address-1-state} {address-1-zip} {address-1-country}
                            );
    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;
}

function wpmudev_get_acf_field_prefilled_sierra_segura( $html, $field ){
if( !is_user_logged_in() ){
return $html;
}
$user_id = get_current_user_id();
$slug = $field['element_id'];
    $map_fields = array(
                            'text-1' => 'Roadster1Marca',// Marca Roadster
                            'text-2' => 'Roadster1Modelo' // Modelo Roadster
                            );
    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;
}
 
Respondido : 03/03/2023 1:10 am