skip to Main Content

I have been trying to make the billing phone number a required component, but have been unable to affect it in any way. Here’s what I have tried so far

using hooks in functions.php

add_filter('woocommerce_billing_fields', 'no_billing_phone_validation' );
function no_billing_phone_validation( $fields ) {
    $fields['billing_phone']['required'] = true;
    return $fields;
}

Setting the phone to required in my theme

enter image description here

and using the plugin "Checkout Field Editor for WooCommerce" to set the phone to required

enter image description here

but despite all of this, the billing phone is still optional, and I am able to check out without adding a phone number. Is there anything that I’m missing?

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    In the end i was able to find a solution. The following file defines the object for the billing field woocommerce/src/blocks/domains/services/checkoutfields.php. That file had the following object

            'phone'      => array(
                'label'          => __( 'Phone', 'woocommerce' ),
                'optionalLabel'  => __(
                    'Phone (optional)',
                    'woocommerce'
                ),
                'required'       => true,
                'hidden'         => false,
                'type'           => 'tel',
                'autocomplete'   => 'tel',
                'autocapitalize' => 'characters',
                'index'          => 100,
            ),
    

    Confusingly, if I set required to true, this did NOT actually make the field required. There must be some function overriding that elsewhere. So what I did was delete the phone object and make an identical phone2 object. This was able to be set to required


  2. How can I add a new field to the new guttenberg Checkout bock? Thanks to the method you put I managed to add the field, but I can’t get it to be reflected in the invoice.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search