Skip to content

Instantly share code, notes, and snippets.

@sardbaba
Created March 1, 2016 19:27
Show Gist options
  • Save sardbaba/83a13e9239008ecc147e to your computer and use it in GitHub Desktop.
Save sardbaba/83a13e9239008ecc147e to your computer and use it in GitHub Desktop.
Woocommerce - Aggiunge verifica email ai campi billing
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');
}
}
}
}
@sardbaba
Copy link
Author

sardbaba commented Mar 1, 2016

In aggiunta un po' di JS per mostrare il campo non valido (vedi "validate_field" di woocommerce/assets/js/frontend/checkout.js):

    $('form.checkout').on('blur change', '#billing_email_validator', function() {
        var $this = $(this);
        if ($this.val() == '' || $this.val().toLowerCase() != $('#billing_email').val().toLowerCase()) {
            setTimeout(function(){
                $('#billing_email_validator_field').removeClass('woocommerce-validated').addClass( 'woocommerce-invalid woocommerce-invalid-email' );
            }, 10);
        }
    });

Il timeout è necessario per entrare poco dopo la validate di woocommerce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment