Home / Archive / WPForms: swap price and option value for payment fields
Duplicate Snippet

Embed Snippet on Your Site

WPForms: swap price and option value for payment fields

This snippet makes the option label "$40.00 - Second Choice" instead of "Second Choice - $40.00" for different payment fields.

Code Preview
php
<?php
add_filter( 'wpforms_html_field_value', function ( $value, $field, $form_data, $context ) {
	if ( 'email-html' !== $context ) {
		return $value;
	}
	$form_id = (int) $form_data['id'];
	// For a specific form.
	// REMOVE IF NEEDED FOR ALL FORMS.
	if ( 1 !== $form_id ) {
		return $value;
	}
	// Different field types have different data format.
	switch ( $field['type'] ) {
		case 'payment-select':
			$value = wpforms_format_amount( $field['amount_raw'], true ) . ' - ' . $field['value_choice'];
			break;
		case 'payment-single':
			$value = wpforms_format_amount( $field['amount_raw'], true ) . ' - ' . $field['name'];
			break;
		case 'payment-multiple':
			$field_submit = $field['value_raw'];
			if (
				! empty( $field_submit ) &&
				! empty( $field['choices'][ $field_submit ]['value'] ) &&
				! empty( $field['choices'][ $field_submit ]['label'] )
			) {
				$amount = wpforms_format_amount( wpforms_sanitize_amount( $field['choices'][ $field_submit ]['value'] ), true );
				$value  = $amount . ' - ' . sanitize_text_field( $field['choices'][ $field_submit ]['label'] );
			}
			break;
	}
	return $value;
}, 15, 4 );

Comments

Add a Comment