We have a claim form (worked with Elementor) and we want each claim received to have a unique code.
We will work with a correlative code such as R001, R002.
- Copy this code into the WordPress function.php file.
- In order to identify the form:
- Add the ID“form_name” to the form.
- Add the name “Claim Book” to the form.
- We must modify the prefix of our database. In this case we use “CJf2B6_” but usually you have something like “wp_”.
- Also in the same line we must modify the name of the database table that hosts our claims. In this case we are using the elementor submission so we use “_e_submissions” and “_e_submissions_values”.
- If you want to render the Claim code generated in any field of the Form, you must add the Field and give it the ID “reclamos_id”.
// Libro de Reclamaciones - Código único secuencial
add_action( 'elementor_pro/forms/validation', 'project_libro_reclamaciones_codigo_unico', 10, 2 );
function project_libro_reclamaciones_codigo_unico( $record, $handler ) {
global $wpdb;
// Verificar que se envíe desde el formulario del libro de reclamaciones.
$form_name = $record->get_form_settings( 'form_name' );
if($form_name != 'Libro de reclamaciones'){
return;
}
// Buscamos el último lead enviado
$fv_form = $wpdb->get_results("SELECT id FROM CJf2B6_e_submissions WHERE element_id = 'acdb477' ORDER BY id DESC LIMIT 1");
// Tomamos el código
$fv_val = $wpdb->get_results("SELECT value FROM CJf2B6_e_submissions_values WHERE submission_id = ". $fv_form[0]->id ." AND `key` = 'reclamos_id'");
// Si es el primer lead, se crear el primer código de la secuencia, sino, sumamos uno
if($fv_val){
$fv_id = (int)preg_replace('/[^0-9]/', '',$fv_val[0]->value);
$reclamo_num = $fv_id + 1;
}else{
$reclamo_num = 1;
}
// Agregamos el código con el id aumentado, el LR puede variar según tu necesidad
$reclamo_id = 'LR-'.sprintf("%04d", $reclamo_num);
// Actualizamos el input hidden donde renderizamos el id del reclamo generado
$record->update_field('reclamos_id', 'value', $reclamo_id);
$record->update_field('reclamos_id', 'raw_value', $reclamo_id);
}