Mostafa
Mostafa

Reputation: 197

sanitizing phone number input woocommerce

I need to block some phone number on woocommerce checkout page

if the phone numbers started with 0111 or 0222 or 0333 it should prompt an error message.


By default WooCommerce checkout fields support the following attributes for the fields

$defaults = array(
'type'              => 'text',
'label'             => '',
'description'       => '',
'placeholder'       => '',
'maxlength'         => false,
'required'          => false,
'id'                => $key,
'class'             => array(),
'label_class'       => array(),
'input_class'       => array(),
'return'            => false,
'options'           => array(),
'custom_attributes' => array(),
'validate'          => array(),
'default'           => '',
);

for now i could only set "maxlength" numbers with below function

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{        
  $fields['billing']['billing_phone']['maxlength'] = 10;      
  return $fields;    
}

Upvotes: 0

Views: 1110

Answers (1)

Temani Afif
Temani Afif

Reputation: 272909

you may try something like this :

add_action('woocommerce_checkout_process', 'phoneValidate');

function phoneValidate() {
    $billing_phone = filter_input(INPUT_POST, 'billing_phone');

    if ( /* you condition */) {
        wc_add_notice(__('Invalid Phone Number.'), 'error');
    }
}

Upvotes: 1

Related Questions