在 WooCommerce 中选择“本地取货”时,将结帐字段设为可选
Posted
技术标签:
【中文标题】在 WooCommerce 中选择“本地取货”时,将结帐字段设为可选【英文标题】:Make checkout fields optional when selecting "Local Pickup" in WooCommerce 【发布时间】:2020-07-26 07:53:59 【问题描述】:我在选择“本地取货”送货方式时使用了隐藏不必要字段的代码。
在这种情况下,当我选择“本地取货”时,我的地址字段会被隐藏。
// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script()
// HERE your shipping methods rate IDs
$local_pickup = 'local_pickup:2';
$pickpoint = 'wc_custom_shipping_pickpoint';
$required_text = esc_attr__( 'required', 'woocommerce' );
$required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
?>
<script>
jQuery(function($)
var ism = 'input[name^="shipping_method"]', ismc = ism+':checked',
csa = 'input#ship-to-different-address-checkbox',
rq = '-required', vr = 'validate'+rq, w = 'woocommerce', wv = w+'-validated',
iv = '-invalid', fi = '-field', wir = w+iv+' '+w+iv+rq+fi,
b = '#billing_', s = '#shipping_', f = '_field',
a1 = 'country', a2 = 'address_1', a3 = 'address_2', a4 = 'postcode', a5 = 'state', a6 = 'city',
b1 = b+a1+f, b2 = b+a2+f, b3 = b+a3+f, b4 = b+a4+f, b5 = b+a5+f, b6 = b+a6+f,
s1 = s+a1+f, s2 = s+a2+f, s3 = s+a3+f, s4 = s+a4+f, s5 = s+a5+f, s6 = s+a6+f,
pickPoint = '<?php echo $pickpoint; ?>', localPickup = '<?php echo $local_pickup; ?>';
// Utility function to shows or hide checkout fields
function showHide( action='show', selector='' )
if( action == 'show' )
$(selector).show(function()
$(this).addClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() == undefined )
$(selector+' label').append('<?php echo $required_html; ?>');
);
else
$(selector).hide(function()
$(this).removeClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() != undefined )
$(selector+' label > .required').remove();
);
// Initializing at start after checkout init (Based on the chosen shipping method)
setTimeout(function()
if( $(ismc).val() == pickPoint ) // Chosen "Pick point" (Hiding "Delivery")
showHide('hide',b1 ); // Country
showHide('hide',b2 ); // Address 1
showHide('hide',b3 ); // Address 2
showHide('hide',b4 ); // Postcode
showHide('hide',b5 ); // State
showHide('hide',b6 ); // City
else if( $(ismc).val() == localPickup ) // Choosen "Local pickup" (Hidding "Take away")
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
else
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function()
if( $(ismc).val() == pickPoint )
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
if( $(csa).prop('checked') )
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
else if( $(ismc).val() == localPickup )
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
showHide('hide',b6);
if( $(csa).prop('checked') )
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
else
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
showHide('show',b6);
if( $(csa).prop('checked') )
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('hide',s6);
);
// When "shipping to different address" is changed (Live event)
$(csa).click( function()
if( $(ismc).val() == pickPoint && $(this).prop('checked') )
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',b6);
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
else if( $(ismc).val() == localPickup && $(this).prop('checked') )
showHide('hide',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',b6);
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',s6);
else
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b4);
showHide('show',b6);
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
showHide('show',s6);
);
);
</script>
<?php
但不幸的是,我有一个问题。地址字段仍然是必需的,不允许下订单。
我尝试将它们设为可选,但没有任何效果。
add_filter( 'woocommerce_billing_fields', 'wc_optional_billing_fields', 10, 1 );
function wc_optional_billing_fields( $address_fields )
$address_fields['billing_address_1']['required'] = false;
return $address_fields;
如何更改代码,以便在选择“本地取货”时地址字段变为可选?
我很乐意为您提供帮助!
【问题讨论】:
【参考方案1】:你试过了吗:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields($address_fields)
$address_fields['address_1']['required'] = false;
$address_fields['address_1']['placeholder'] = '';
$address_fields['address_2']['required'] = false;
$address_fields['address_2']['placeholder'] = '';
return $address_fields;
【讨论】:
以上是关于在 WooCommerce 中选择“本地取货”时,将结帐字段设为可选的主要内容,如果未能解决你的问题,请参考以下文章
在 WooCommerce 2.6 和 3+ 中删除特定类别的统一运费方法