forza11 Posted November 12, 2022 Share Posted November 12, 2022 Hello there, I use below code snippet in woocommerce to confirm emails. It creates a php error as below. How I can solve it? "[09-Nov-2022 07:55:09 UTC] PHP Warning: Undefined array key "confirm_email" in /srv/htdocs/wp-content/themes/generatepress_child/functions.php on line 846" /** * Snippet Name: WooCommerce Confirm Email On Registration Form * Snippet Author: ecommercehints.com */ // Create the field and display it on the registraiton form add_action('woocommerce_register_form', 'ecommercehints_registraion_form_confirm_email_field'); function ecommercehints_registraion_form_confirm_email_field() { woocommerce_form_field( 'confirm_email', array( 'type' => 'text', 'required' => true, 'label' => 'E-posta adresi tekrar', ), (isset($_POST['confirm_email']) ? $_POST['confirm_email'] : '') ); } // Show an error message if the confirm email field is empty or doesn't match the billing email field add_action('woocommerce_register_post', 'ecommercehints_confirm_email_field_validation', 10, 3); function ecommercehints_confirm_email_field_validation($username, $email, $errors) { if (empty($_POST['confirm_email'])) { $errors->add('confirm_email_error', 'Lütfen e-posta adresinizi iki alana da girin!'); } if ( $email !== $_POST['confirm_email'] ) { $errors->add('confirm_email_error', 'Girdiğiniz e-posta adresleri birbiri ile aynı değil!'); } } Quote Link to comment https://forums.phpfreaks.com/topic/315524-undefined-array-key-error/ Share on other sites More sharing options...
Solution maxxd Posted November 12, 2022 Solution Share Posted November 12, 2022 In your function ecommercehints_confirm_email_field_validation() (yeesh, wordpress really promotes bad ides...), your second if() statement needs to be an elseif() branch. The reason is that in the first if() you're checking to make sure $_POST['confirm_email'] is set, which is correct. In the second, however, you're assuming it is set by trying to compare it to another value. So if you make it an elseif branch it'll only run when the variable is set, which is the behavior you want. Quote Link to comment https://forums.phpfreaks.com/topic/315524-undefined-array-key-error/#findComment-1602500 Share on other sites More sharing options...
forza11 Posted November 12, 2022 Author Share Posted November 12, 2022 2 hours ago, maxxd said: In your function ecommercehints_confirm_email_field_validation() (yeesh, wordpress really promotes bad ides...), your second if() statement needs to be an elseif() branch. The reason is that in the first if() you're checking to make sure $_POST['confirm_email'] is set, which is correct. In the second, however, you're assuming it is set by trying to compare it to another value. So if you make it an elseif branch it'll only run when the variable is set, which is the behavior you want. Thanks for reply. So you suggest to change this code if ( $email !== $_POST['confirm_email'] ) { to this one, right? elseif ( $email !== $_POST['confirm_email'] ) { Quote Link to comment https://forums.phpfreaks.com/topic/315524-undefined-array-key-error/#findComment-1602505 Share on other sites More sharing options...
maxxd Posted November 12, 2022 Share Posted November 12, 2022 It looks like that'll do what you're looking for, though obviously I've not tested it. Quote Link to comment https://forums.phpfreaks.com/topic/315524-undefined-array-key-error/#findComment-1602544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.