Introduction
Would you like to hide items with zero quantities from your WPForms notification emails when using Dropdown Items or Single Items payment fields? When using these payment fields with quantity options enabled, by default WPForms shows all selected items in notifications regardless of their quantity.
In this tutorial, we’ll show you how to hide items that have zero quantities from your notification emails.
Adding the Snippet
To hide zero-quantity items from your payment field notifications, you’ll need to add a custom snippet to your site. If you need help with adding snippets to your site, check out our tutorial on adding PHP or JavaScript for WPForms.
/**
* Hiding Zero Quantity Items in Email Notifications for Payment Fields
*
* @link https://wpforms.com/developers/how-to-hide-zero-quantity-items-in-dropdown-payment-field-notifications
*/
add_filter('wpforms_entry_email_data', function ($fields, $entry, $form_data) {
foreach ($fields as $field_id => $field) {
// Adjust to handle 'payment-single', 'payment-select', or any other types
if (
isset($form_data['fields'][$field_id]['type']) &&
in_array($form_data['fields'][$field_id]['type'], ['payment-select', 'payment-single'])
) {
$quantity = isset($field['quantity']) ? (int) $field['quantity'] : 0;
if ($quantity == 0) {
unset($fields[$field_id]);
}
}
}
return $fields;
}, 10, 3);
This snippet will check each payment field in your form notification and retrieve the quantity value and removes any field that has a quantity of zero, while preserving those with quantities greater than zero in your notification emails.
And that’s it! You’ve successfully hidden zero-quantity items from your payment field notifications.
Would you like to customize your notification emails further? Take a look at our article on increasing image size in notification emails.