简体   繁体   中英

Remove (optional) text from fields on My account edit address in Woocommerce 3.4+

I'm trying to remove the <span class="optional">(optional)</span> from the WooCommerce My Account edit address page. Is there an other way to do it like this?

.optional {
    display: none;
}

I think it would be better to remove it completely from the DOM in the form.

How can I do this?

To remove " (optional) " label text from fields in my account edit address, use the following code:

// Remove "(optional)" from  non required fields (in My account edit address)
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
    if( is_wc_endpoint_url( 'edit-address' ) ){
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

Related: Remove "(optional)" text from checkout fields in Woocommerce 3.4+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM