从Woocommerce 3.4中的条件结帐字段中删除“(可选)”文本

我以前使用这个答案来隐藏基于选择的送货方式的结帐字段,它工作正常,直到更新(3.4.2当前版本)我认为不确定什么已更改但它不再按预期工作。

以前当选择本地拾取时,某些字段被隐藏并且是可选的,并且当选择传送时,它将再次通过动态显示这些字段而不重新加载页面。

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

在此处输入图像描述

这是我修改过的剪贴板:

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' ); ?>  jQuery(function($){ var shippingMethod = $('input[name^="shipping_method"]:checked'), required = '<abbr class="required" title="">*', 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() == '' ) { 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() == '' || '') { 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() == '' ) { 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() == '' || '') { 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() == '' && 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() == '' || '' && 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'); } }); });  <?php } 

任何指针将不胜感激!

更新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() ) ) return $field; $optional = ' (' . esc_html__( 'optional', 'woocommerce' ) . ')'; $keys = array( 'billing_country', 'billing_address_1', 'billing_state', 'billing_postcode', 'shipping_country', 'shipping_address_1', 'shipping_state', 'shipping_postcode' ); if( in_array($key, $keys) ) return str_replace( $optional, '', $field ); else 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 = ' (' . esc_html__( 'optional', 'woocommerce' ) . ')'; ?>   

代码位于活动子主题(或活动主题)的function.php文件中。 经过测试,适用于Woocommerce版本3.4+。

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