Thread Starter
Xaifu
(@xaifu)
I haven’t been able to find a solution for this issue anywhere.
I think I have checked the whole internet with no luck.
Does anyone in this forum feel the spirit of Christmas and is willing to help?
😉
I have the exact same problem – I need to show the WooCommerce countries dropdown on my registration form. Please someone help us 😀
I also need to show the WooCommerce countries dropdown on my registration form.
Anyone found a solution ?
I encountered the same scenario a while ago and manage to solve by doing what is directed in
this site mentioned by OP with some modification to add the billing_country in the registration page.
To display the country in registration page:
function wooc_extra_register_fields() {
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries');
?>
<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_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
<select class="country_select" name="billing_country" id="reg_billing_country">
<?php foreach ($countries as $key => $value): ?>
<option value="<?php echo $key?>"><?php echo $value?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
add_action( 'woocommerce_register_form', 'wooc_extra_register_fields' );
To save the fields:
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_country'] ) ) {
// WooCommerce billing country
update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Of course you can follow the code on the tutorial to include custom validation for these fields.
Hope this helps!
Regards.
Thread Starter
Xaifu
(@xaifu)
Thanks for the code felmantbuntog
I can’t check it as I had to make a workaround registering the country as the company name in order to solve the problem.
I can assure you that if this piece of code you are sharing works, it will be very useful for people looking for a solution. I spent several days looking for something like that and there was absolutely nothing.
thanks felmantbuntog
I can confirm This code does work and is very useful !
I wish woocommerce would document this.
I am glad to hear that this works for you too. Your welcome.
@felmantbuntog
Thanks so much for posting that snippet, it was just what I was looking for.
Any suggestions for displaying and saving the State/Province on the registration page?