我在woocommerce注册上添加了一个自定义字段,该字段实际上通过使用以下放在子主题的function.php文件上的代码来保存客户IG用户名:
function wc_extra_register_fields() { ?>
<p class="form-row form-row-wide">
<label for="reg_instagram_username"><?php _e( 'Your Instagram Username', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="instagram_username" id="reg_instagram_username" value="<?php esc_attr_e( $_POST['instagram_username'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wc_extra_register_fields' );
function wc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if (isset($_POST['instagram_username']) && empty($_POST['instagram_username']) ) {
$validation_errors->add('instagram_username_error', __('Your Instagram username is required!', 'woocommerce'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'wc_validate_extra_register_fields', 10, 3);
function wc_save_extra_register_fields($customer_id) {
if (isset($_POST['instagram_username'])) {
update_user_meta($customer_id, 'instagram_username', sanitize_text_field($_POST['instagram_username']));
}
}
add_action('woocommerce_created_customer', 'wc_save_extra_register_fields');
用户名正在作为自定义字段添加到我的数据库中。但是,我真正想做的是将它保存在用户的“联系信息”中的“ Instagram”字段下。
是否可以从注册中提取此信息并将其直接保存为用户元?
非常感谢您的帮助。
谢谢!