自定义woocommerce注册表

时间:2021-06-21 19:15:09

标签: wordpress woocommerce

我想将手机号码字段(作为必填字段)添加到我的 woocommerce 商店。 是否有任何硬编码方法可以在登录/注册表格上启用相同的功能?它还会显示在我的帐户页面上吗(成功创建帐户后)? 提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要将以下代码放在当前活动主题的functions.php中。

/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
   <label for="billing_phone_number"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
   <input type="text" class="input-text" name="billing_phone_number" id="billing_phone_number" value="<?php if (!empty($_POST['billing_phone_number'])) esc_attr_e($_POST['billing_phone_number']); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['billing_phone_number']) && empty($_POST['billing_phone_number'])) {
$validation_errors->add('billing_phone_number_error', __('<strong>Error</strong>: First 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_phone_number'])) {
update_user_meta($customer_id, 'phone_number', sanitize_text_field($_POST['billing_phone_number']));
update_user_meta($customer_id, 'billing_phone_number', sanitize_text_field($_POST['billing_phone_number']));
}    
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');

相关问题