Remove “(optional)” text from checkout fields in Woocommerce 3.4+

前端 未结 2 1539
甜味超标
甜味超标 2020-12-05 20:34

I was previously using this answer to hide checkout fields based on chosen shipping method, it worked fine until an update (3.4.2 current version) I think not sure what has

相关标签:
2条回答
  • 2020-12-05 21:26

    Update 2

    To remove "(optional)" text from checkout fields labels introduced with Woocommerce release 3.4, just as it was before, you will need to add the following code:

    // PHP: Remove "(optional)" from our non required fields
    add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
    function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
        // Only on checkout page
        if( is_checkout() && ! is_wc_endpoint_url() ) {
            $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
            $field = str_replace( $optional, '', $field );
        }
        return $field;
    }
    
    // JQuery: Needed for checkout fields to Remove "(optional)" from our non required fields
    add_filter( 'wp_footer' , 'remove_checkout_optional_fields_label_script' );
    function remove_checkout_optional_fields_label_script() {
        // Only on checkout page
        if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        ?>
        <script>
        jQuery(function($){
            // On "update" checkout form event
            $(document.body).on('update_checkout', function(){
                $('#billing_country_field label > .optional').remove();
                $('#billing_address_1_field label > .optional').remove();
                $('#billing_postcode_field label > .optional').remove();
                $('#billing_state_field label > .optional').remove();
                $('#shipping_country_field label > .optional').remove();
                $('#shipping_address_1_field label > .optional').remove();
                $('#shipping_postcode_field label > .optional').remove();
                $('#shipping_state_field label > .optional').remove();
            });
        });
        </script>
        <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works in Woocommerce version 3.4+.

    You could merge the included jQuery code with your existing jQuery code…

    0 讨论(0)
  • 2020-12-05 21:29

    you can easily use css in this

    .woocommerce form .form-row .required{
        display: none ;
    }
    
    .woocommerce form .form-row .optional{
        display: none ;
    }
    
    0 讨论(0)
提交回复
热议问题