如何将自定义字段添加到WooCommerce注册表单中 [英] How to add custom fields to WooCommerce registration form
问题描述
我正在尝试向WooCommerce注册表单中添加自定义字段,特别是名字和姓氏领域。我已经设法创建这些字段,但输入的信息在用户登录时不会传递到帐户详细信息页面。其他教程提到验证字段,但我不确定它与我是否相关。我正在研究一个WordPress的儿童主题。
请访问键盘.org 来查看代码。
我试图通过使用代码示例选项粘贴代码,但它不能正常工作。
希望我已经清楚地解释了我自己。如果没有,请让我知道,我会澄清。
我认为你已经覆盖 woocommerce / templates /myaccount/form-login.php
模板,通过这样做,您已经设法显示 billing_first_name
和 billing_last_name
但您忘记使用 woocommerce_created_customer
将这些数据保存到数据库中所需的钩子。
我会建议您将模板保持原样并添加这些字段 function.php
以下是在WooCommerce注册表单中添加自定义字段的代码:
/ **
*添加WooCommerce注册表单自定义字段。
* /
function text_domain_woo_reg_form_fields(){
?>
< p class =form-row form-row-first>
< label for =billing_first_name><?php _e('First name','text_domain'); ?>< span class =required> *< / span>< / label>
< input type =textclass =input-textname =billing_first_nameid =billing_first_namevalue =<?php if(!empty($ _ POST ['billing_first_name'])) esc_attr_e($ _ POST ['billing_first_name']);?> />
< / p>
< p class =form-row form-row-last>
< label for =billing_last_name><?php _e('Last name','text_domain'); ?>< span class =required> *< / span>< / label>
< input type =textclass =input-textname =billing_last_nameid =billing_last_namevalue =<?php if(!empty($ _ POST ['billing_last_name'])) esc_attr_e($ _ POST ['billing_last_name']);?> />
< / p>
< div class =clear>< / div>
<?php
}
add_action('woocommerce_register_form_start','text_domain_woo_reg_form_fields');
关于验证问题的第二部分是完全可选的,取决于您的业务逻辑你想要什么,一般来说大部分网站都需要名字和姓氏,但它又完全取决于你,如果你不想验证它,那么删除< span class =required > *< / span>
并跳过此部分。
/ **
*验证WooCommerce注册表单自定义字段。
* /
函数text_domain_woo_validate_reg_form_fields($ username,$ email,$ validation_errors){
if(isset($ _ POST ['billing_first_name'])&& empty($ _ POST ['billing_first_name '])){
$ validation_errors-> add('billing_first_name_error',__('< strong>错误< / strong> ;:需要名字!','text_domain'));
if(isset($ _ POST ['billing_last_name'])&& empty($ _ POST ['billing_last_name'])){
$ validation_errors-> add('billing_last_name_error',__('< strong>错误< / strong>:姓氏是必需的!','text_domain'));
}
返回$ validation_errors;
}
add_action('woocommerce_register_post','text_domain_woo_validate_reg_form_fields',10,3);
现在这是一个主要部分,这是你错过了,下面的代码是需要保存自定义数据:
/ **
*保存WooCommerce注册表单自定义字段。
* /
函数text_domain_woo_save_reg_form_fields($ customer_id){
//名字字段
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']));
//姓氏字段
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']));
}
}
add_action('woocommerce_created_customer','text_domain_woo_save_reg_form_fields');
以上所有代码都放在 function.php
您活动的儿童主题(或主题)的文件。或者也可以在任何插件php文件中使用。
代码已经过测试并且功能完善。
希望这有助于您!
I have seen similar questions but couldn't find a solution for me.
I am trying to add custom fields to WooCommerce registration form, specifically first and last name field. I have managed to create these fields but the information entered does not pass over to the Account Details page when the user has logged in. Other tutorials have mentioned validating the fields but I am not sure is it relevant to me or not. I am working on a Wordpress child theme.
Please visit to codepad .org to view the code. I tried to paste the code here by using the code sample option but it doesn't work properly.
Hope I have explained myself clearly. If not please let me know and I will clarify.
I think you have overwrite woocommerce/templates/myaccount/form-login.php
template, and by doing that you have managed to show the billing_first_name
and billing_last_name
but you forget to use woocommerce_created_customer
hook which is needed to save those data into your database.
What I'll suggest is that you keep the template as it is and add those field through function.php
Here is the code to add custom fields in WooCommerce registration form:
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="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="billing_last_name"><?php _e('Last name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
Coming to your second part of your question on validation it is totally optional and depends on your business logic that what you want, in general most of the site has First Name and the Last Name required but again it totally depends upon you, If you don't want to validate this then remove <span class="required">*</span>
from above code and skip this section.
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_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!', 'text_domain'));
}
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!.', 'text_domain'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
Now this is a main part and this what you was had missed, below code is needed to save the custom data:
/**
* To save WooCommerce registration form custom 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']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
All the above code goes in function.php
file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.
Hope this helps!
这篇关于如何将自定义字段添加到WooCommerce注册表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!