wordpress - wordpress注册中的条件字段
问题描述
我使用下面的代码向我的网站添加了一些自定义注册字段。
- 我还希望添加一个“国家”选择字段。需要将什么代码添加到下面的代码中才能添加“帐单国家/地区”选择下拉选项。
- 我想添加一个条件,如果选择了“美国”,那么只有这样,“许可证号”和“agd”这两个字段才会显示,否则将保持隐藏状态。
请告知并让我知道我的问题是否足够清楚。先感谢您 :)
function wooc_extra_register_fields() {?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<div class="clear"></div>
<p class="form-row form-row-wide">
<label for="reg_license_number"><?php _e( 'License Number', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="license_number" id="reg_license_number" value="<?php esc_attr_e( $_POST['license_number'] ); ?>" />
</p>
<div class="clear"></div>
<p class="form-row form-row-wide">
<label for="reg_agd"><?php _e( 'AGD ID', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="agd" id="reg_agd" value="<?php esc_attr_e( $_POST['agd'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* register fields Validating.
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['reg_billing_phone'] ) && empty( $_POST['reg_billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_phone'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['billing_first_name'] ) ) {
//First name field which is by default
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// First name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// Last name field which is by default
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['license_number'] ) ) {
// Last name field which is by default
update_user_meta( $customer_id, 'license_number', sanitize_text_field( $_POST['license_number'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'license_number', sanitize_text_field( $_POST['license_number'] ) );
}
if ( isset( $_POST['agd'] ) ) {
// Last name field which is by default
update_user_meta( $customer_id, 'agd', sanitize_text_field( $_POST['agd'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'agd', sanitize_text_field( $_POST['agd'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
解决方案
您好,建议使用此插件https://wordpress.org/plugins/header-footer/并在页脚中添加一个 jquery 脚本,该脚本在选择“美国”时显示“许可证号”和“agd”。
我举个例子
jQuery(.license).hide();
jQuery(.agd).hide();
var selected_option = $('select#country option:selected');
$('select#country').on('change', function() {
if(selected_option=='united states') {
jQuery(.license).show();
jQuery(.agd).show();
} else {
jQuery(.license).hide();
jQuery(.agd).hide();
}
});
让我知道
推荐阅读
- r - 向 DF 添加额外的行
- python - 如何保存 LDA 模型 - Python 中的 LatentDirichletAllocation
- c# - 数据表第一列需要mm/dd/yyyy格式
- python - 在每个循环中酸洗和加载熊猫数据框以节省进度……坏主意?
- r - 如何将截距拟合到 ggplot 线性模型图中的特定值?
- ibm-datapower - 如何在 Datapower 中配置 Json Settings 对象
- f# - 如何使用 FSharp.Compiler.Services 的结果
- c# - 为什么 WebSocket4Net 使用 1011 错误代码关闭连接
- c++ - 为什么我不能在 Windows 的 rabbitmq 中创建队列?
- javascript - 为什么我的 setState 函数会覆盖当前状态?