Woocommerce Validation

Hi,


I wanted that Checkout on Woocommerce could validate email.

Like:

Email:

Confirm email:


And if they don't match, a error would be given.


I tried this on other theme and works: https://www.wpdesk.net/docs/validate-woocommerce-checkout-fields


Just installing that plugin and adding the following code to functions.php, but on this theme I get header error when adding the code:


/** * Can validate field by comparing value with other field. */class WPDesk_FCF_Validation_Confirm_Field { /** * Field to compare with validated field. * * @var string */ private $field_to_compare; /** * WPDesk_FCF_Validation_Confirm_Field constructor. * * @param string $field_to_compare . */ public function __construct( $field_to_compare ) { $this->field_to_compare = $field_to_compare; } public function hooks() { add_filter( 'flexible_checkout_fields_custom_validation', array( $this, 'register_custom_validation' ) ); } /** * Register custom validation. * * @param array $custom_validation . * * @return array */ public function register_custom_validation( $custom_validation ) { $custom_validation[ 'field_confirmation_' . $this->field_to_compare ] = array( 'label' => sprintf( __( 'Compare with %1$s', 'wpdesk' ), $this->field_to_compare ), 'callback' => array( $this, 'validate' ) ); return $custom_validation; } /** * Validate. * * @param string $field_label Field label. * @param string $value Field value. */ public function validate( $field_label, $value ) { $field_to_compare_value = sanitize_text_field( $_POST[ $this->field_to_compare ] ); $valid = $field_to_compare_value === $value; if ( ! $valid ) { wc_add_notice( sprintf( __( 'Invalid %1$s value.', 'wpdesk' ), '<strong>' . $field_label . '</strong>' ), 'error' ); } }}$fcf_validation_confirm_field = new WPDesk_FCF_Validation_Confirm_Field( 'billing_email' );$fcf_validation_confirm_field->hooks();

Comments

Sign In or Register to comment.