如何将自定义字段添加到WooCommerce注册表中
我看到过类似的问题,但找不到解决方案。如何将自定义字段添加到WooCommerce注册表中
我正在尝试向WooCommerce注册表单中添加自定义字段,具体是名字和姓氏字段。我已经设法创建这些字段,但输入的信息在用户登录时不会传递到“帐户详细信息”页面。其他教程提到验证字段,但我不确定它与我是否相关。我正在研究一个WordPress的儿童主题。
请访问codepad .org查看代码。 我试图通过使用代码示例选项粘贴代码,但它无法正常工作。
希望我已经清楚地解释了我自己。如果没有,请让我知道,我会澄清。
您可以使用此插件在宇商登记表添加自定义字段
Woocommerce额外注册字段
https://wordpress.org/plugins/woocommerce-extra-accounts-fields/screenshots/
感谢
我认为你必须覆盖woocommerce/templates/myaccount/form-login.php
模板,并通过这样做,你已经设法显示billing_first_name
和billing_last_name
但您忘记使用woocommerce_created_customer
挂钩,这需要将这些数据保存到数据库中。
我会建议为你保持模板,因为它是通过function.php
添加这些字段下面是代码以WooCommerce登记表添加自定义字段:
/**
* 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');
来到你的问题的第二部分验证它是完全可选的,并且取决于你的业务逻辑,你想要什么,一般来说大多数网站都需要名字和姓氏,但它又完全取决于你,如果你不想要验证它,然后从上面的代码中删除<span class="required">*</span>
并跳过这一步 部分。
/**
* 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);
现在,这是一个主要组成部分,这一点,你是错过了什么,下面需要的代码来保存自定义数据:
/**
* 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');
以上所有的代码都在function.php
文件的活跃儿童主题(或主题)。或者也可以在任何插件php文件中使用。
该代码已经过测试并且功能齐全。
希望这有助于!