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>
<div class="clear"></div>
<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 if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
<select name="country" >
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AU">Australia</option>
<option value="IL">Israel</option>
<option value="IT">Italy</option>
</select>
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* Validate the extra register fields.
*
* @param string $username Current username.
* @param string $email Current email.
* @param object $validation_errors WP_Error object.
*
* @return void
*/
function wooc_validate_extra_register_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!', 'woocommerce' ) );
}
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!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Save the extra register fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// WooCommerce billing last name.
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['billing_phone'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['country'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'country', sanitize_text_field( $_POST['country'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
if ($_POST['country']=='IT') {
$current_user = wp_get_current_user();
$user_data = array(
'ID' => $current_user->ID,
'role' => 'New Role Here'
);
wp_update_user( $user_data );
}
我正在开发插件,它将在我的帐户页面中添加额外的字段(用户注册)。我会加上名字,姓氏,电话号码和国家。但我的主要目标是根据国家的情况分配使用过的角色如果用户选择了意大利,那么角色应该是意大利(我已经在后端创建了这个角色)
你能建议我如何实现
编写用于在woommerce_created_customer钩子中添加用户角色的代码。
function wooc_save_extra_register_fields($customer_id)
{
if (isset($_POST['billing_first_name'])) {
// WordPress default first name field.
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
// WooCommerce billing first name.
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
if (isset($_POST['billing_last_name'])) {
// WordPress default last name field.
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
// WooCommerce billing last name.
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
if (isset($_POST['billing_phone'])) {
// WooCommerce billing phone
update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']));
}
if (isset($_POST['country'])) {
// WooCommerce billing phone
update_user_meta($customer_id, 'country', sanitize_text_field($_POST['country']));
// Assign user role
switch ($_POST['country']) {
case 'IT':
wp_update_user(array(
'ID' => $customer_id,
'role' => 'editor' // Update to desired role
));
break;
}
}
}
add_action('woocommerce_created_customer', 'wooc_save_extra_register_fields');