我在填写通过Woocommerce注册页面上的自定义字段获得的信息时遇到问题,填写表单时,似乎唯一可以解决的问题是:名字,姓氏,城市,邮政编码,电话和电子邮件地址 .
当用户注册我们的网站时,他们无法立即登录,因为他们的帐户需要得到管理员的批准,所以提取的信息将用于在他们的帐户被激活之前联系该人 .
我尝试使用 var_dump($_POST)
转储 $_POST
数组,但在进行更改后,没有一个字段正常工作 .
请参阅下面我正在使用的添加自定义字段和填充帐户页面上的字段的内容:
这就是我用来将自定义字段添加到我的woocommerce注册表单中的内容:
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_company"><?php _e( 'Company', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php esc_attr_e( $_POST['billing_company'] ); ?>" /></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>
<p class="form-row form-row-wide">
<label for="reg_billing_adress_1"><?php _e( 'Adress Line 1', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_adress_1" id="reg_billing_adress_1" value="<?php esc_attr_e( $_POST['billing_address_1'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_adress_2"><?php _e( 'Adress Line 2', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_adress_2" id="reg_billing_adress_2" value="<?php esc_attr_e( $_POST['billing_address_2'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php esc_attr_e( $_POST['billing_city'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_postcode"><?php _e( 'Postcode', 'woocommerce' ); ?><span class="required"> *</span></label>
<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php esc_attr_e( $_POST['billing_postcode'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
这是用户在注册后用于填充后端“用户”区域的内容:
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
//Phone Field
if (isset($_POST['billing_phone'])) {
update_user_meta($customer_id, 'phone', sanitize_text_field($_POST['billing_phone']));
update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']));
}
//Billing Adress 1 Field
if (isset($_POST['billing_adress_1'])) {
update_user_meta($customer_id, 'address_1', sanitize_text_field($_POST['billing_address_1']));
update_user_meta($customer_id, 'billing_address_1', sanitize_text_field($_POST['billing_address_1']));
}
//Billing Adress 2 Field
if (isset($_POST['billing_adress_2'])) {
update_user_meta($customer_id, 'address_2', sanitize_text_field($_POST['billing_address_2']));
update_user_meta($customer_id, 'billing_address_2', sanitize_text_field($_POST['billing_address_2']));
}
//Billing City Field
if (isset($_POST['billing_city'])) {
update_user_meta($customer_id, 'city', sanitize_text_field($_POST['billing_city']));
update_user_meta($customer_id, 'billing_city', sanitize_text_field($_POST['billing_city']));
}
//Billing Postcode Field
if (isset($_POST['billing_postcode'])) {
update_user_meta($customer_id, 'postcode', sanitize_text_field($_POST['billing_postcode']));
update_user_meta($customer_id, 'billing_postcode', sanitize_text_field($_POST['billing_postcode']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
我不是最有经验的PHP,所以任何帮助都会受到大力赞赏 .
1 回答
你的第一个钩子函数有一些错误 . 此外,不应要求“公司”和“地址2”字段 .
在您的第二个挂钩功能中,“公司”缺失,因此无论如何都不会保存 .
完成您需要一个额外的钩子函数来进行必要的字段验证 .
所以你的代码应该是:
此代码位于活动子主题(或主题)的function.php文件中 . 经过测试和工作 .