Home / Admin / How to Display Hidden Single Item Fields in Order Summary
Duplicate Snippet

Embed Snippet on Your Site

How to Display Hidden Single Item Fields in Order Summary

This code snippet will display the hidden single item field in order summary

<10
Code Preview
php
<?php
/**
 * Display Hidden Single Item Fields in Order Summary
 *
 * @link https://wpforms.com/developers/how-to-display-hidden-single-item-fields-in-order-summary
 */
function wpf_include_singleitem_hiddentype_include_in_order_summary_footer($foot) {
    // replace the 1000 with your Form ID
    $form_data = wpforms()->form->get( 1000, [ 'content_only' => true ] ); // Replace with actual method to get your form data if needed
    // Loop through form fields to find hidden payment-single fields
    foreach ($form_data['fields'] as $field_id => $field) {
        if ($field['type'] === 'payment-single' && isset($field['format']) && $field['format'] === 'hidden') {
            // Get price for hidden items
            $price = !empty($field['price']) ? wpforms_sanitize_amount($field['price']) : 0;
            $foot[] = [
                'label'    => isset($field['label']) ? sanitize_text_field($field['label']) : __('Hidden Item', 'your-text-domain'),
                'quantity' => 1,  // Adjust quantity logic as needed
                'amount'   => wpforms_format_amount($price, true),
                'class'    => 'wpforms-order-summary-hidden-item',
            ];
        }
    }
    return $foot;
}
add_filter('wpforms_forms_fields_payment_total_field_order_summary_preview_foot', 'wpf_include_singleitem_hiddentype_include_in_order_summary_footer');
function wpf_include_singleitem_hiddentype_in_order_summary($fields, $entry, $form_data) {
    $payment_fields = wpforms_payment_fields(); // Retrieve all payment-related fields, typically available in WPForms.
    foreach ($form_data['fields'] as $id => $field) {
        if (in_array($field['type'], $payment_fields, true) && $field['type'] === 'payment-single' && isset($field['format']) && $field['format'] === 'hidden') {
            // Calculate price only if the format is hidden
            $price = !empty($field['price']) ? wpforms_sanitize_amount($field['price']) : 0;
            $fields[$id] = [
                'name'       => sanitize_text_field($field['label']),
                'value'      => wpforms_format_amount($price, true),
                'amount'     => wpforms_format_amount($price),
                'amount_raw' => $price,
                'type'       => 'payment-single-hidden',
            ];
        }
    }
    return $fields;
}
add_filter('wpforms_forms_fields_payment_total_field_order_summary', 'wpf_include_singleitem_hiddentype_in_order_summary', 10, 3);

Comments

Add a Comment