php - 根据在 WooCommerce 中选择的城市显示或隐藏结帐邮政编码字段
问题描述
在 Woocommerce 中,如果选择了特定的城市字段,我会尝试隐藏结帐邮政编码字段。如果公司字段为空,我发现了一个隐藏计费电话的工作代码:
add_action( 'woocommerce_after_checkout_form', 'conditionally_hide_show_checkout_field', 9999 );
function conditionally_hide_show_checkout_field() {
wc_enqueue_js( "
jQuery('#billing_company').keyup(function() {
if (jQuery(this).val().length == 0) {
jQuery('#billing_phone').hide();
} else {
jQuery('#billing_phone').show();
}
}).keyup();
");
}
我不知道如何更改此代码以在选择特定计费城市时隐藏所需的邮政编码字段。任何帮助表示赞赏。
解决方案
它有点复杂,您需要在最后一个函数中定义城市。
以下代码处理多项必要任务,以根据特定定义的选定城市显示/隐藏邮政编码字段:
// Make postcode field optional
add_filter( 'woocommerce_default_address_fields', 'customizing_checkout_fields' );
function customizing_checkout_fields( $fields ) {
if( is_checkout() ) {
$fields['postcode']['required'] = false;
}
return $fields;
}
// Replace "(optional)" text by required "*"
add_filter( 'woocommerce_form_field' , 'replace_checkout_optional_by_required', 10, 4 );
function replace_checkout_optional_by_required( $field, $key, $args, $value ) {
// Only on checkout page for postcode field
if( is_checkout() && ! is_wc_endpoint_url() && in_array($key, ['billing_postcode', 'shipping_postcode']) ) {
$optional = '<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$required = '<abbr class="required" title="required">*</abbr>';
$field = str_replace( $optional, $required, $field );
}
return $field;
}
// Hidden input fields for Postcode validation when it's visible
add_action( 'woocommerce_after_order_notes', 'checkout_hidden_field_for_validation', 10, 1 );
function checkout_hidden_field_for_validation( $checkout ) {
// Hidden field for the phone number validation
echo '<input type="hidden" name="billing_postcode_check" id="billing_postcode_check" value="">
<input type="hidden" name="shipping_postcode_check" id="shipping_postcode_check" value="">';
}
// Postcode field validation when it's visible
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
if( isset($_POST['billing_postcode_check']) && $_POST['billing_postcode_check'] === 'yes'
&& isset($_POST['billing_postcode']) && empty($_POST['billing_postcode']) ) {
wc_add_notice( __("The billing postcode field is a required field.", "woocommerce"), 'error' );
}
if( isset($_POST['shipping_postcode_check']) && $_POST['shipping_postcode_check'] === 'yes'
&& isset($_POST['shipping_postcode']) && empty($_POST['shipping_postcode']) ) {
wc_add_notice( __("The shipping postcode is a required field.", "woocommerce"), 'error' );
}
}
// Conditional Show hide checkout fields based on chosen payment methods
add_action( 'wp_footer', 'conditionally_show_hide_billing_custom_field' );
function conditionally_show_hide_billing_custom_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
// HERE define the city that will hide postcode field
$city = 'Paris';
$required = ' <abbr class="required" title="required">*</abbr>';
?>
<script>
jQuery(function($){
var bc = '#billing_city_field input',
sc = '#shipping_city_field input',
bp = '#billing_postcode_field',
sp = '#shipping_postcode_field',
bpc = '#billing_postcode_check',
spc = '#shipping_postcode_check',
city = '<?php echo $city; ?>',
req = '<?php echo $required; ?>';
// On "update" checkout form event, replace "(optional)" by required "*"
$(document.body).on('update_checkout', function(){
$('#billing_postcode_field label > .optional').replaceWith(req);
});
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' ){
var check = selector == bp ? $(bpc) : $(spc);
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
check.val('yes');
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
check.val('');
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// 1. Initialising (on load): Show/hide based on customer city
// Billing field
if( $(bc).val() === city || $(bc).val() == '' )
showHide( bp, 'hide' );
else
showHide( bp );
// Shipping field
if( $(sc).val() === city || $(sc).val() == '' )
showHide( sp, 'hide' );
else
showHide( sp );
// 2. Change live event: Show/hide based on city change
$( 'form.checkout' ).on( 'change input', bc, function() { // Billing field
if( $(bc).val() === city )
showHide( bp, 'hide' );
else
showHide( bp );
});
$( 'form.checkout' ).on( 'change input', sc, function() { // Shipping field
if( $(sc).val() === city )
showHide( sp, 'hide' );
else
showHide( sp );
});
});
</script>
<?php
endif;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
一些相关的答案:
在加载时隐藏帐单和运输邮政编码:
代替:
// 1. Initialising (on load): Show/hide based on customer city
// Billing field
if( $(bc).val() === city || $(bc).val() == '' )
showHide( bp, 'hide' );
else
showHide( bp );
// Shipping field
if( $(sc).val() === city || $(sc).val() == '' )
showHide( sp, 'hide' );
else
showHide( sp );
经过:
// 1. Initialising (on load): Hide postcode
showHide( bp, 'hide' ); // Billing field
showHide( sp, 'hide' ); // Shipping field
推荐阅读
- python - 我应该担心(在运行 ~/.local/bin/virtualenvwrapper.sh 之后)“找不到命令,但可以安装:...”(Ubuntu 桌面)?
- python - 当我的开始屏幕被触发时,我的游戏计时器没有被清除
- titanium - 要求语句不能与 Titanium SDK 8.0.0.GA 一起正常工作
- python-3.x - 如何使用 matplotlib 绘制包含水平和垂直箱线图的坐标图
- javascript - 使用数组对项目进行排序
- python - 如何将模拟对象传递给自定义 simple_tag 的单元测试?
- r - 为长格式数据集中的每个参与者寻找随时间变化的斜率
- twitter-bootstrap - 为什么引导边距这么大,两边15px,总结30px?
- python - 从 Pandas 数据框中的不同列创建索引列
- graphviz - 使用 Graphviz 创建互连圆图