Validate phone number in woocommerce checkout page

后端 未结 2 383
清歌不尽
清歌不尽 2021-01-16 18:36

I would like to add custom validation to the phone number in checkout page of woocommerce. How do I do this??

2条回答
  •  礼貌的吻别
    2021-01-16 19:14

    BY default woocommerce already have regular expression validation. The easiest way would be to validate it through jquery.

    EDIT: Try not to make any changes to the woocommerce core as they will be over ridden on next update. Try this code

    // Hook in
    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
         $fields['shipping']['shipping_phone'] = array(
            'label'     => __('Phone', 'woocommerce'),
        'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
         );
    
         return $fields;
    }
    

提交回复
热议问题