我之前使用this answer根据选择的运送方式来隐藏结帐字段,它工作得很好,直到更新(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' );
?>
<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
}
如果有任何建议,我们将不胜感激!
发布于 2018-12-21 06:39:19
您可以很容易地在这里使用css
.woocommerce form .form-row .required{
display: none ;
}
.woocommerce form .form-row .optional{
display: none ;
}
发布于 2021-01-12 21:48:27
更好的解决方案:
/**
* 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 = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
https://stackoverflow.com/questions/50769727
复制相似问题