Created
March 1, 2016 19:27
-
-
Save sardbaba/83a13e9239008ecc147e to your computer and use it in GitHub Desktop.
Woocommerce - Aggiunge verifica email ai campi billing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class My_Class { | |
public function __construct () { | |
// Aggiunge verifica email ai campi billing | |
add_filter( 'woocommerce_billing_fields' , array($this, 'woocommerce_billing_fields'), 10, 1 ); | |
add_action( 'woocommerce_checkout_process', array($this, 'woocommerce_checkout_process'), 9 ); | |
} | |
function array_insert(&$array, $position, $insert) { | |
$pos = array_search($position, array_keys($array)); | |
$array = array_merge( | |
array_slice($array, 0, $pos), | |
$insert, | |
array_slice($array, $pos) | |
); | |
} | |
function woocommerce_billing_fields( $fields ) { | |
if ( !is_user_logged_in() ) { | |
$this->array_insert( | |
$fields, | |
'billing_phone', | |
array( | |
'billing_email_validator' => array( | |
'type' => 'text', | |
'label' => 'Conferma email', | |
'placeholder' => '', | |
'required' => true, | |
) | |
) | |
); | |
} | |
return $fields; | |
} | |
function woocommerce_checkout_process() { | |
if ( !is_user_logged_in() ) { | |
if (isset($_POST['billing_email']) && isset($_POST['billing_email_validator']) | |
&& strtolower($_POST['billing_email']) != strtolower($_POST['billing_email_validator'])) { | |
wc_add_notice("L'email inserita e la conferma non corrispondono", 'error'); | |
} | |
elseif (isset($_POST['billing_email']) && !isset($_POST['billing_email_validator'])) { | |
wc_add_notice("Si prega di confermare l'indirizzo email", 'error'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In aggiunta un po' di JS per mostrare il campo non valido (vedi "validate_field" di woocommerce/assets/js/frontend/checkout.js):
Il timeout è necessario per entrare poco dopo la validate di woocommerce.