Issue with Checkbox Fields in E2PDF Template

23 February, 2025 02:18:19
eliodata
Topics: 19
Messages: 57
Hi,

I am having trouble configuring checkbox fields in my E2PDF template to reflect the selections made in a CF7 form. Here are the details of my setup:

CF7 Form Configuration:

I have a select field in my CF7 form with the following configuration:
[select select-587 "Option 1" "Option 2" "Option 3"]

E2PDF Template Configuration:

The form is located on the WooCommerce customer order page. Once validated, the order status changes, and the PDF is attached to the email sent to the customer.

I have successfully configured text fields in the E2PDF template to display the first and last name of the signer using the following syntax:

Signataire : [e2pdf-wc-order key="45012_form_prenom" meta="true"] [e2pdf-wc-order key="45012_form_nom" meta="true"]
However, when I try to configure checkboxes to reflect the selection from the select field, the checkboxes do not get checked. Here is what I have tried:

Value: [e2pdf-wc-order key="45012_select-587" meta="true"]
Option: Option 1

Expected Behavior:

I expect the checkboxes in the E2PDF template to be checked based on the selection made in the CF7 select field.

Current Behavior:

The checkboxes remain unchecked regardless of the selection in the CF7 form.

Could you please guide me on how to correctly configure the checkboxes in the E2PDF template to reflect the selections from the CF7 select field? Any assistance or examples would be greatly appreciated.

Thank you for your help.
23 February, 2025 02:44:35
E2Pdf
Support
Topics: 7
Messages: 3438
Hello,

Could you please create a temporary "textarea" in the E2Pdf Template and map it with the following "Value": [e2pdf-wc-order key="45012_select-587" meta="true"]? Then, let us know what output you get upon exporting the PDF.
We would really appreciate your feedback at WordPress.org!
23 February, 2025 04:03:35
eliodata
Topics: 19
Messages: 57
Thank you for your help!

"Value": [e2pdf-wc-order key="45012_select-587" meta="true"] render nothing

I tried to add the select to this snippet which works well for the other fields but without result, maybe you 'll know how :

// Enregistrer l'URL de la signature et du fichier joint dans les métadonnées de la commande avant l'envoi du formulaire pour template e2pdf devis et conventions
add_action('wpcf7_before_send_mail', 'cf7oss_plugin_save_signature_url_and_attachment_to_order_metadata');
function cf7oss_plugin_save_signature_url_and_attachment_to_order_metadata($contact_form)
{
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();

if (isset($posted_data['order_id'])) {
$order_id = absint($posted_data['order_id']);
$order = wc_get_order($order_id);
if ($order) {
$form_id = $contact_form->id();
$signature_field = '';
$stamp_field = '';
$meta_signature_key = '';
$meta_file_key = '';
$prefix = '';

// Déterminer les champs, les clés de métadonnées, et le préfixe selon le formulaire utilisé
if ($form_id == '259494') {
// Configuration pour le formulaire 259494
$signature_field = 'signature-800';
$stamp_field = 'tampon-520cfdb7_file';
$meta_signature_key = '_signature_259494';
$meta_file_key = '_file_259494';
$prefix = '259494_'; // Préfixe pour distinguer les champs texte
} elseif ($form_id == '45012') {
// Configuration pour le formulaire 45012
$signature_field = 'signature-700';
$stamp_field = 'tampon-420cfdb7_file';
$meta_signature_key = '_signature_45012';
$meta_file_key = '_file_45012';
$prefix = '45012_'; // Préfixe pour distinguer les champs texte
} elseif ($form_id == '260960') {
// Configuration pour le formulaire 260960
$signature_field = 'signature-900';
$stamp_field = 'tampon-620cfdb7_file';
$meta_signature_key = '_signature_260960';
$meta_file_key = '_file_260960';
$prefix = '260960_'; // Préfixe pour distinguer les champs texte
}

// Enregistrer la signature si disponible
if (isset($posted_data[$signature_field])) {
$signature_url = $posted_data[$signature_field];
$order->update_meta_data($meta_signature_key, $signature_url);
}

// Ajouter les champs texte aux métadonnées de la commande avec un préfixe
if (isset($posted_data['fsbdd_form-prenom'])) {
$order->update_meta_data($prefix . 'form_prenom', $posted_data['fsbdd_form-prenom']);
}
if (isset($posted_data['fsbdd_form-nom'])) {
$order->update_meta_data($prefix . 'form_nom', $posted_data['fsbdd_form-nom']);
}
if (isset($posted_data['fsbdd_form-fonction'])) {
$order->update_meta_data($prefix . 'form_fonction', $posted_data['fsbdd_form-fonction']);
}


// Récupérer les données enregistrées par CF7DB et traiter le fichier tampon
global $wpdb;
$cfdb = apply_filters('cfdb7_database', $wpdb);
$table_name = $cfdb->prefix . 'db7_forms';
$results = $cfdb->get_results(
$cfdb->prepare("SELECT * FROM $table_name WHERE form_post_id = %d ORDER BY form_id DESC LIMIT 1", $form_id)
);

if (!empty($results)) {
$result = reset($results);
$form_data = unserialize($result->form_value);

if (isset($form_data[$stamp_field])) {
$upload_dir = wp_upload_dir();
$attachment_file_name = $form_data[$stamp_field];
$attachment_url = $upload_dir['baseurl'] . '/cfdb7_uploads/' . $attachment_file_name;

// Enregistrer l'URL du fichier joint
$order->update_meta_data($meta_file_key, $attachment_url);
}
}

// Sauvegarder les modifications dans la commande
$order->save();
}
}
}
23 February, 2025 04:22:29
E2Pdf
Support
Topics: 7
Messages: 3438
If you see an empty value and do not see the value in the WooCommerce Orders page, then there is likely an issue with the data when you are trying to write it to your order using your custom PHP code.

You need to debug your code step by step to identify where it fails. In your code, we do not see the part responsible for saving the current field. However, we suspect that the issue might be due to storing an array. If storing the array fails, you can try converting the value from Contact Form 7 into an inline format using commas (e.g., Option 1, Option 2, Option 3) and save it directly.
We would really appreciate your feedback at WordPress.org!
23 February, 2025 05:31:20
eliodata
Topics: 19
Messages: 57
I have found a solution that works,

Handling the Select Field in the Code:

To correctly handle the select field data, I add the following code to the snippet:


if (isset($posted_data[$select_field])) {
$raw_value = $posted_data[$select_field];

// Unserialize if necessary
$unserialized_value = maybe_unserialize($raw_value);
if (is_array($unserialized_value)) {
$clean_value = reset($unserialized_value); // Take the first value of the array
} else {
$clean_value = $raw_value;
}

$order->update_meta_data($meta_select_key, $clean_value);
}
Configuring the Value Field in E2PDF:

For the Value field in the E2PDF template, I used the following syntax to ensure the data is correctly unserialized and used:

[e2pdf-format-output unserialize=1][e2pdf-wc-order key="45012_select" meta="true"]
This solution ensures that the checkboxes in the E2PDF template are checked based on the selection made in the CF7 select field.

Thank you for your assistance!

Best regards,