删除“(可选)"Woocommerce 3.4+ 结账字段中的文本 [英] Remove "(optional)" text from checkout fields in Woocommerce 3.4+

查看:22
本文介绍了删除“(可选)"Woocommerce 3.4+ 结账字段中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用

下面是我修改后的狙击手:

add_filter('woocommerce_default_address_fields', 'custom_default_checkout_fields', 10, 1 );函数 custom_default_checkout_fields( $address_fields ) {$custom_fields = array('country', 'address_1', 'address_2', 'state', 'postcode');foreach($custom_fields 作为 $field)$address_fields[$field]['required'] = false;返回 $address_fields;}add_action('wp_footer', 'custom_checkout_field_script');函数 custom_checkout_field_script() {$pickpoint = 'local_pickup:2';$free_delivery = 'free_shipping:1';$flat_rate = 'flat_rate:3';$required = esc_attr__('required', 'woocommerce');?><脚本>jQuery(函数($){var shippingMethod = $('input[name^="shipping_method"]:checked'),required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',ShippingChecked = $('input#ship-to-different-address-checkbox');运输检查.更改(功能(){console.log('发货检查:'+shippingChecked.prop('checked'));});函数 showHide( actionToDo='show', selector='' ){if( actionToDo == 'show' )$(选择器).show(function(){$(this).addClass("validate-required");$(this).removeClass("woocommerce-validated");$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");if( $(selector+' > label > abbr').html() == undefined )$(selector+'label').append(required);});别的$(选择器).隐藏(功能(){$(this).removeClass("validate-required");$(this).removeClass("woocommerce-validated");$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");if( $(selector+' > label > abbr').html() != undefined )$(selector+' label > .required').remove();});}if(shippingMethod.val() == '<?php echo $pickpoint; ?>' ){showHide('show','#billing_country_field');showHide('隐藏','#billing_address_1_field');showHide('隐藏','#billing_address_2_field');showHide('隐藏','#billing_postcode_field');showHide('隐藏','#billing_state_field');}else if( shippingMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>'){showHide('show','#billing_address_1_field');showHide('show','#billing_address_2_field');showHide('show','#billing_postcode_field');showHide('隐藏','#billing_state_field');showHide('隐藏','#billing_country_field');}$( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {var shipMethod = $('input[name^="shipping_method"]:checked');if( shipMethod.val() == '<?php echo $pickpoint; ?>' ){showHide('show','#billing_country_field');showHide('隐藏','#billing_address_1_field');showHide('隐藏','#billing_address_2_field');showHide('隐藏','#billing_postcode_field');showHide('隐藏','#billing_state_field');}else if( shipMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>'){showHide('show','#billing_address_1_field');showHide('show','#billing_address_2_field');showHide('show','#billing_postcode_field');showHide('隐藏','#billing_state_field');showHide('隐藏','#billing_country_field');}别的{showHide('show','#billing_address_1_field');showHide('show','#billing_address_2_field');showHide('show','#billing_postcode_field');showHide('show','#billing_state_field');showHide('show','#billing_country_field');}});$( 'input#ship-to-不同地址-checkbox' ).click( function() {var shipMethod = $('input[name^="shipping_method"]:checked');if( shipMethod.val() == '<?php echo $pickpoint; ?>' && shippingChecked.prop('checked') == true ){showHide('show','#billing_country_field');showHide('隐藏','#billing_address_1_field');showHide('隐藏','#billing_address_2_field');showHide('隐藏','#billing_postcode_field');showHide('隐藏','#billing_state_field');showHide('show','#shipping_country_field');showHide('隐藏','#shipping_address_1_field');showHide('隐藏','#shipping_address_2_field');showHide('隐藏','#shipping_postcode_field');showHide('隐藏','#shipping_state_field');}else if( shipMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>' &&shippingChecked.prop('checked')== 真){showHide('show','#billing_address_1_field');showHide('show','#billing_address_2_field');showHide('show','#billing_postcode_field');showHide('隐藏','#billing_state_field');showHide('隐藏','#billing_country_field');showHide('show','#shipping_address_1_field');showHide('show','#shipping_address_2_field');showHide('show','#shipping_postcode_field');showHide('隐藏','#shipping_state_field');showHide('隐藏','#shipping_country_field');}else if( shippingChecked.prop('checked') == false ){showHide('show','#shipping_address_1_field');showHide('show','#shipping_address_2_field');showHide('隐藏','#shipping_state_field');showHide('隐藏','#shipping_country_field');}});});<?php}

任何指针将不胜感激!

解决方案

更新 2

要从 Woocommerce 3.4 版引入的结帐字段标签中删除 (可选)" 文本,就像以前一样,您需要添加以下代码:

//PHP:从我们的非必填字段中删除(可选)"add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );函数 remove_checkout_optional_fields_label( $field, $key, $args, $value ) {//仅在结账页面if( is_checkout() && !is_wc_endpoint_url() ) {$optional = '<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';$field = str_replace( $optional, '', $field );}返回 $field;}//JQuery:结帐字段需要从我们的非必填字段中删除(可选)"add_filter( 'wp_footer' , 'remove_checkout_optional_fields_label_script' );函数 remove_checkout_optional_fields_label_script() {//仅在结账页面if( ! ( is_checkout() && !is_wc_endpoint_url() ) ) 返回;$optional = '<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';?><脚本>jQuery(函数($){//在更新"结帐表单事件上$(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();});});<?php}

代码位于活动子主题(或活动主题)的 function.php 文件中.已在 Woocommerce 3.4+ 版中测试并运行.

<块引用>

您可以将包含的 jQuery 代码与现有的 jQuery 代码合并...

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 changed but it doesn't work as intended anymore.

Previously when local pickup was chosen some fields were hidden and made optional and when delivery was chosen it would show those fields again all via dynamically without reloading the page.

Now it shows and hides the fields as required however, when delivery is chosen it is showing the correct fields marked as mandatory but also has the (optional) sign next to it and it makes it optional. See picture below.

Here's my modified snipper below:

add_filter('woocommerce_default_address_fields', 'custom_default_checkout_fields', 10, 1 );
function custom_default_checkout_fields( $address_fields ) {
$custom_fields = array( 'country', 'address_1', 'address_2', 'state', 'postcode');
foreach($custom_fields as $field)
    $address_fields[$field]['required'] = false;
return $address_fields;
}

add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {

$pickpoint = 'local_pickup:2';
$free_delivery = 'free_shipping:1';
$flat_rate = 'flat_rate:3';

$required = esc_attr__( 'required', 'woocommerce' );
?>
<script>
    jQuery(function($){

        var shippingMethod = $('input[name^="shipping_method"]:checked'),
            required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
            shippingChecked = $('input#ship-to-different-address-checkbox');

        shippingChecked.change( function(){
            console.log('Shipping Checked: '+shippingChecked.prop('checked'));
        });

        function showHide( actionToDo='show', selector='' ){
            if( actionToDo == 'show' )
                $(selector).show(function(){
                    $(this).addClass("validate-required");
                    $(this).removeClass("woocommerce-validated");
                    $(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
                    if( $(selector+' > label > abbr').html() == undefined )
                        $(selector+' label').append(required);
                });
            else
                $(selector).hide(function(){
                    $(this).removeClass("validate-required");
                    $(this).removeClass("woocommerce-validated");
                    $(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
                    if( $(selector+' > label > abbr').html() != undefined )
                        $(selector+' label > .required').remove();
                });
        }

        if( shippingMethod.val() == '<?php echo $pickpoint; ?>' )
        {
            showHide('show','#billing_country_field' );
            showHide('hide','#billing_address_1_field' );
            showHide('hide','#billing_address_2_field' );
            showHide('hide','#billing_postcode_field' );
            showHide('hide','#billing_state_field' );
        }
        else if( shippingMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>')
        {
            showHide('show','#billing_address_1_field');
            showHide('show','#billing_address_2_field');
            showHide('show','#billing_postcode_field');
            showHide('hide','#billing_state_field');
            showHide('hide','#billing_country_field');
        }

        $( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {
            var shipMethod = $('input[name^="shipping_method"]:checked');
            if( shipMethod.val() == '<?php echo $pickpoint; ?>' )
            {
                showHide('show','#billing_country_field');
                showHide('hide','#billing_address_1_field');
                showHide('hide','#billing_address_2_field');
                showHide('hide','#billing_postcode_field');
                showHide('hide','#billing_state_field');
            }
            else if( shipMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>')
            {
                showHide('show','#billing_address_1_field');
                showHide('show','#billing_address_2_field');
               showHide('show','#billing_postcode_field');
                showHide('hide','#billing_state_field');
                showHide('hide','#billing_country_field');
            }
            else
            {
                showHide('show','#billing_address_1_field');
                showHide('show','#billing_address_2_field');
                showHide('show','#billing_postcode_field');
                showHide('show','#billing_state_field');
                showHide('show','#billing_country_field');
            }
        });

        $( 'input#ship-to-different-address-checkbox' ).click( function() {
            var shipMethod = $('input[name^="shipping_method"]:checked');
            if( shipMethod.val() == '<?php echo $pickpoint; ?>' && shippingChecked.prop('checked') == true )
            {
                showHide('show','#billing_country_field');
                showHide('hide','#billing_address_1_field');
                showHide('hide','#billing_address_2_field');
                showHide('hide','#billing_postcode_field');
                showHide('hide','#billing_state_field');

                showHide('show','#shipping_country_field');
                showHide('hide','#shipping_address_1_field');
                showHide('hide','#shipping_address_2_field');
                showHide('hide','#shipping_postcode_field');
                showHide('hide','#shipping_state_field');
            }
            else if( shipMethod.val() == '<?php echo $free_delivery; ?>' || '<?php echo $flat_rate; ?>' && shippingChecked.prop('checked') == true )
            {
                showHide('show','#billing_address_1_field');
                showHide('show','#billing_address_2_field');
                showHide('show','#billing_postcode_field');
                showHide('hide','#billing_state_field');
                showHide('hide','#billing_country_field');

                showHide('show','#shipping_address_1_field');
                showHide('show','#shipping_address_2_field');
                showHide('show','#shipping_postcode_field');
                showHide('hide','#shipping_state_field');
                showHide('hide','#shipping_country_field');
            }
            else if( shippingChecked.prop('checked') == false )
            {
                showHide('show','#shipping_address_1_field');
                showHide('show','#shipping_address_2_field');
                showHide('hide','#shipping_state_field');
                showHide('hide','#shipping_country_field');
            }
        });
    });
</script>
<?php
}

Any pointers would be much appreciated!

解决方案

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…

这篇关于删除“(可选)"Woocommerce 3.4+ 结账字段中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆