phpwordpresswoocommercecheckoutrequired

How to remove asterisk from WooCommerce checkout billing email field?


I managed to change the email address field to be optional via the code below, but I can't seem to be able to remove the asterisk on the checkout page.

enter image description here

// Make billing email field optional at checkout
add_filter( 'woocommerce_billing_fields', 'make_billing_email_optional' );
function make_billing_email_optional( $address_fields ) {
    $address_fields['billing_email']['required'] = false;
    return $address_fields;
}

// Clear billing email when it is not provided to avoid type error
add_filter( 'woocommerce_checkout_posted_data', 'clear_billing_email' );
function clear_billing_email( $order_data ) {
    if ( isset( $order_data['billing_email'] ) && empty( $order_data['billing_email'] ) ) {
        $order_data['billing_email'] = '';
    }
    return $order_data;
}

Solution

  • You can use the following, to remove the (required) asterisk from specific field:

    // PHP: Remove required asterisk from specific field
    add_filter( 'woocommerce_form_field' , 'remove_checkout_required_asterisk_field', 10, 4 );
    function remove_checkout_required_asterisk_field( $field, $key, $args, $value ) {
        // Only on checkout page and billing email field
        if( is_checkout() && ! is_wc_endpoint_url() && $key === 'billing_email' ) {
            $required = '&nbsp;<abbr class="required" title="required">*</abbr>';
            $field = str_replace( $required, '', $field );
        }
        return $field;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.