Php 删除“;(可选)“;Woocommerce 3.4+;

Php 删除“;(可选)“;Woocommerce 3.4+;,php,jquery,wordpress,woocommerce,shipping,Php,Jquery,Wordpress,Woocommerce,Shipping,我以前使用的是基于所选的发货方法隐藏签出字段,在更新(3.4.2当前版本)之前,它工作得很好。我想不知道发生了什么变化,但它不再像预期的那样工作 以前,当选择本地拾取时,一些字段被隐藏并成为可选字段,而当选择交付时,它将通过动态方式再次显示这些字段,而无需重新加载页面 现在,它会根据需要显示和隐藏字段。但是,当选择交付时,它会显示标记为必填的正确字段,但旁边还有(可选)符号,并使其成为可选字段。请参见下图 下面是我修改过的snipper: add_filter('woocommerce_def

我以前使用的是基于所选的发货方法隐藏签出字段,在更新(3.4.2当前版本)之前,它工作得很好。我想不知道发生了什么变化,但它不再像预期的那样工作

以前,当选择本地拾取时,一些字段被隐藏并成为可选字段,而当选择交付时,它将通过动态方式再次显示这些字段,而无需重新加载页面

现在,它会根据需要显示和隐藏字段。但是,当选择交付时,它会显示标记为必填的正确字段,但旁边还有(可选)符号,并使其成为可选字段。请参见下图

下面是我修改过的snipper:

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
}
add_filter('woocommerce_default_address_fields','custom_default_checkout_fields',10,1);
函数自定义\默认\签出\字段($address\字段){
$custom_fields=数组('country','address_1','address_2','state','postcode');
foreach($custom_字段作为$field)
$address_fields[$field]['required']=false;
返回$address_字段;
}
添加_操作(“wp_页脚”、“自定义_签出_字段_脚本”);
函数自定义\签出\字段\脚本(){
$pickpoint='local_pickup:2';
$免费送货='免费送货:1';
$flat_rate='flat_rate:3';
$required=esc_attr_uuuuuuuuuuu('required','woocommerce');
?>
jQuery(函数($){
var shippingMethod=$('input[name^=“shipping_method”]:checked'),

必需='更新2

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

// 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
}
//PHP:Remove“(可选)”从非必填字段中删除
添加过滤器('woocommerce\u form\u field','remove\u checkout\u optional\u fields\u label',10,4);
函数删除\签出\可选\字段\标签($field,$key,$args,$value){
//仅在结帐页面上
if(is_checkout()&&!is_wc_endpoint_url()){
$optional='('.esc_html_uuuuuuuu('optional','woocommerce'));
$field=str_replace($optional,,$field);
}
返回$field;
}
//JQuery:签出字段需要从非必填字段中删除“(可选)”
添加过滤器(“wp\u页脚”、“删除\u签出\u可选\u字段\u标签\u脚本”);
函数删除\签出\可选\字段\标签\脚本(){
//仅在结帐页面上
if(!(is_checkout()&&!is_wc_endpoint_url())返回;
$optional='('.esc_html_uuuuuuuu('optional','woocommerce'));
?>
jQuery(函数($){
//关于“更新”签出表单事件
$(document.body).on('update\u checkout',function(){
$('#账单\国家\字段标签>.optional')。删除();
$(“#账单地址_1_字段标签>.optional”).remove();
$(“#账单(邮政编码)字段标签>.optional”).remove();
$(“#账单状态_字段标签>.optional”).remove();
$(“#装运国家/地区字段标签>.optional”).remove();
$(“#配送地址_1_字段标签>.optional”).remove();
$(“#配送(邮政编码)字段标签>.optional”).remove();
$(“#装运状态_字段标签>.optional”).remove();
});
});

在这种情况下,您可以轻松地使用css

.woocommerce form .form-row .required{
    display: none ;
}

.woocommerce form .form-row .optional{
    display: none ;
}
更好的解决方案:

/**
 * Remove optional label
 * https://elextensions.com/knowledge-base/remove-optional-text-woocommerce-checkout-fields/
 */
add_filter( 'woocommerce_form_field' , 'elex_remove_checkout_optional_text', 10, 4 );
function elex_remove_checkout_optional_text( $field, $key, $args, $value ) {
    if( is_checkout() && ! is_wc_endpoint_url() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}
/**
*删除可选标签
* https://elextensions.com/knowledge-base/remove-optional-text-woocommerce-checkout-fields/
*/
添加过滤器('woocommerce\u form\u field'、'elex\u remove\u checkout\u optional\u text',10,4);
函数elex\u remove\u checkout\u可选\u文本($field、$key、$args、$value){
if(is_checkout()&&!is_wc_endpoint_url()){
$optional='('.esc_html_uuuuuuuu('optional','woocommerce'));
$field=str_replace($optional,,$field);
}
返回$field;
}

@LoicTheAztec感谢您更新它,因为它使字段成为可选的,而实际上不隐藏任何字段。无论选择哪种方法。包括传递在内的任何选项都使字段成为可选的。@AbuNooh抱歉,这太复杂了,结帐页面上真的是一场噩梦。但我终于找到了正确的组合n让它工作。Loic,不需要道歉,也谢谢你花时间看一看。它工作,但有一个小问题,事实上,它删除了可选标签,但仍将这些字段视为可选字段,因此可以通过验证,而无需填写地址、邮政编码和状态字段。我不完全确定如何进行我认为
$address\u字段[$field]['required']=false;
需要以某种方式撤消。再次感谢!我回到Loic,为什么它会这样做?即使字段存在并标记为必填字段?这是否发生在您身上?如果勾选了选项
在输入地址之前隐藏配送方法
,是否有办法使此工作正常进行,因为
配送已勾选:false
出于某种原因禁用AJAX。在我看来,使用CSS永久删除元素不是一个很好的解决方案……但比公认的答案中的JS解决方案要好这对我来说非常有效!我很好奇为什么@GDY注意到使用CSS不是一个好的解决方案。我只是想确保我使用的是最好的方法。@jord8on:这是一个糟糕的风格e、 如果你想移除一些东西,那么就真的移除它,不要让它看不见。你可以考虑“清理你的房间”只是把这些东西放在你的床下:D…但在这种环境下,这通常是最方便可行的解决方案,所以肯定是可用的。但如果这是一个选项,我总是更愿意真正删除这些元素。@gdy:真棒!谢谢分享。我总是想知道为什么!@gdy在这种情况下,隐藏文本“可选”在我看来,使用CSS很好。开销很小,而且很容易逆转。在许多情况下,我同意;最好是完全删除一些内容,尤其是在