Home / Admin / How to Hide Zero Quantity Items in Dropdown Payment Field Notifications
Duplicate Snippet

Embed Snippet on Your Site

How to Hide Zero Quantity Items in Dropdown Payment Field Notifications

This snippet hides items with zero quantities from your WPForms notification emails when using Dropdown Items payment fields

<10
Code Preview
php
<?php
/**
 * Hide zero quantity items from Dropdown Items payment field notifications
 *
 * @link https://wpforms.com/developers/how-to-hide-zero-quantity-items-in-dropdown-payment-field-notifications
 */
add_action('wpforms_loaded', function() {
    add_filter('wpforms_entry_email_data', function ($fields, $entry, $form_data) {
        foreach ($fields as $field_id => $field) {
            if ('payment-select' === $field['type']) {
                // Get the quantity from the quantities array
                $field_number = substr($field_id, -1); // Get the last digit of field ID
                $quantity = isset($entry['quantities'][$field_number]) ? intval($entry['quantities'][$field_number]) : 0;
                
                if ($quantity == 0) {
                    unset($fields[$field_id]);
                    continue;
                }
            }
        }
        return $fields;
    }, 10, 3);
});

Comments

Add a Comment