I need to create My Account page for registered users with changeable fields on backend and frontend.
Now the fields are updated if I change the values through the admin panel. However, if I change the field values through My Account, the fields are not saved or updated.
What do i need to change or add to the code?
I used the following LoicTheAztec's answers:
Add a custom field in Woocommerce Edit Account page
Adding some my account custom fields to admin user pages in Woocommerce
Adding an additional custom field in Woocommerce Edit Account page
My code:
Template:
$user = wp_get_current_user();
<form class="tabs-content__page_inner woocommerce-EditAccountForm edit-account" action="" method="post"
<div class="pers-acc__tab-fields_field tab-field woocommerce-form-row form-row">
<label for="account_phone" class="tab-field__title"><?php esc_html_e( 'Телефон', 'woocommerce' ); ?></label>
<div class="tab-field__input-inner">
<input type="text" class="tab-field__input woocommerce-Input woocommerce-Input--text input-text" name="account_phone" id="account_phone" value="<?php echo esc_attr( $user->account_phone ); ?>" />
</div>
</div>
</form>
functions.php
<?php
add_action('woocommerce_save_account_details', 'save_phone_number_account_details', 20, 1);
function save_phone_number_account_details($user_id) {
if( isset( $_POST['account_phone'] ) ) {
update_user_meta( $user_id, 'account_phone', sanitize_text_field( $_POST['account_phone'] ) );
}
}
//custom field in admin console
add_action( 'show_user_profile', 'add_extra_custom_user_data', 1, 1 );
add_action( 'edit_user_profile', 'add_extra_custom_user_data', 1, 1 );
function add_extra_custom_user_data( $user )
{
?>
<h3><?php _e("Other details",'woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="account_phone"><?php _e( 'Custom phone', 'woocommerce' ); ?></label></th>
<td><input type="text" name="account_phone" value="<?php echo esc_attr(get_the_author_meta( 'account_phone', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
//save custom field value in admin console
add_action( 'personal_options_update', 'save_extra_custom_user_data' );
add_action( 'edit_user_profile_update', 'save_extra_custom_user_data' );
function save_extra_custom_user_data( $user_id )
{
if( ! empty($_POST['account_phone']) )
update_user_meta( $user_id, 'account_phone', sanitize_text_field( $_POST['account_phone'] ) );
}
Related
I customized the user registration process of WooCommerce for this I added some extra fields like date of birth & Mobile number.
I added the form elements like this:
function my_extra_register_fields() {?>
<p class="form-row form-row-wide">
<label for="reg_dob"><?php _e( 'Date of Birth', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="reg_customer_dob" id="reg_customer_dob" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php _e( 'Mobile', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form', 'my_extra_register_fields' );
and I am storing data like this -
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_phone'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['reg_customer_dob'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'customer_dob', sanitize_text_field( $_POST['reg_customer_dob'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Now I am trying to add/show these two details (date of birth & Mobile number) in Edit user page of user menu so that admin can see the details.
image for location is attached
But i am unable to read data in this page.
I am try to using the following hook:
add_action( 'edit_user_profile', 'print_custom_fileds_in_edit_user', 30 );
Any adivce?
To display the field between the contact information you can use the user_contactmethods filter hook opposite edit_user_profile
You just need to make sure the user meta key matches
/**
* Filters the user contact methods.
*
* #since 2.9.0
*
* #param string[] $methods Array of contact method labels keyed by contact method.
* #param WP_User $user WP_User object.
*/
function filter_user_contactmethods( $methods, $user ) {
// Add user contact methods
$methods['customer_dob'] = __( 'Date of Birth', 'your-theme-slug' );
return $methods;
}
add_filter( 'user_contactmethods', 'filter_user_contactmethods', 10, 2 );
I have been able to add a custom text input field, in woocommerce's order details page, but I can't save the data submitted data.
This is the code:
add_action( 'woocommerce_order_details_before_order_table', 'add_custom_Field',10,2 );
function add_custom_Field( $order_id) {
$user = wp_get_current_user();
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo esc_attr( $order_id->custom_URL ); ?>" />
</p>
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php
function submit()
{
update_user_meta( $user_id, 'custom_URL', sanitize_text_field( $_POST['custom_URL'] ) );
echo "Your function on button click is working";
}
if(array_key_exists('test',$_POST)){
submit();
}
}
Do you know what I'm doing wrong?
The following will allow you to add a user custom field to customer order detail and to save its value on submission:
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note based on your comment:
If the custom url has to be different for each order, you can't save it as user meta data, but instead as $order item meta data... But the code will be slightly different and you will need to define how will be your custom urls.
I need to create a custom form in a new tab in woocommerce my account page.¨
I need to save data and output them to the user
What I've tried:
add_action( 'init', 'misha_add_endpoint' );
function misha_add_endpoint() {
add_rewrite_endpoint( 'log-history', EP_PAGES );
}
add_action( 'woocommerce_account_log-history_endpoint', 'misha_my_account_endpoint_content' );
function misha_my_account_endpoint_content() {
get_current_user_id()
echo '<h3>User settings prize wheel</h3>';
$user = wp_get_current_user();
echo '
<form class="woocommerce-EditAccountForm edit-account" action="" method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_display_name">Steam ID <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="steam_id" id="account_display_name" value="'.esc_attr( $user->steam_id ).'"> <span><em>Vaše Steam ID, místo kam se budou posílat výherní item. </em></span>
</p>
<div class="clear"></div>
<p>
<button type="submit" class="woocommerce-Button button" name="save_prize_wheel" value="Uložit změny">Uložit změny</button>
<input type="hidden" name="action" value="save_prize_wheel">
</p>
</form>
';
}
add_action( 'woocommerce_save_prize_wheel', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['steam_id'] ) )
update_user_meta( $user_id, 'steam_id', sanitize_text_field( $_POST['steam_id'] ) );
}
The data did not save.
You miss semi Colin here get_current_user_id() it should be get_current_user_id();
I'm trying to get the encrypted user_pass data from my wp_users table in my database to show up as a custom editable and updatable field (like the email field) in the "Edit User" pages within the admin side only. I have come up with the following code:
if ( is_admin() ) {
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3>Additional Account Information</h3>
<table class="form-table">
<tbody>
<tr>
<th style="width:20.5%!important;">Existing Password (Encrypted)</th>
<td>
<input type="text" name="user_pass" id="user_pass" value="<?php echo esc_attr($user->user_pass) ?>" class="regular-text" />
</td>
</tr>
</tbody>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
$user_id = wp_update_user( array( 'ID' => $user_id, 'user_pass' => $_POST[ 'user_pass' ] ) );
}
}
UPDATE:
The code above is working but when it updates it is re-encrypting the already encrypted password I am copy and pasting. Thereby making the encrypted password the password itself! I just want it to save the exact way I paste it in. Is this even possible?
I want this data so I am able to copy and paste it from one user area to another within my site as they do not share databases, however some users need to be in more than one database with the same information including password. I would like to be able to do that right from the Wordpress Admin areas instead of in phpMyAdmin.
Trying replacing this :
<input type="text" name="user_pass" id="user_pass" value="<?php echo esc_attr( get_userdata( $user->user_pass, $user->ID ) );; ?>" class="regular-text" />
by this :
<input type="text" name="user_pass" id="user_pass" value="<?= esc_attr($user->user_pass) ?>" class="regular-text" />
EDIT :
Here's the code to update the password field in DB :
add_action( 'edit_user_profile_update', 'update_extra_user_fields' );
add_action( 'personal_options_update', 'update_extra_user_fields' );
function update_extra_user_fields($user_id) {
global $wpdb;
if (!current_user_can('edit_user', $user_id)) return;
$user = get_user_by('ID', $user_id);
if (isset($_POST['user_pass']) && !empty($_POST['user_pass'])) {
if ($user->user_pass != $_POST['user_pass']) {
$wpdb->update(
$wpdb->users,
array(
'user_pass' => $_POST['user_pass'],
),
array( 'ID' => $user_id )
);
wp_cache_delete( $user_id, 'users' );
}
}
}
Sorry for the delay!
my question:
How to add mobile phone field in Woocommerce my-account/edit-account/ page
(Related template: form-edit-account.php file)
Like in the following answer thread:
Saving the value of a custom field phone number in WooCommerce My account > Account details
But this answer code is incomplete as there is some missing hooked functions. Any help is appreciated to get something complete, meaning the field display.
You have 3 options to display a custom mobile phone field on My account > Edit account page:
1) As the first field using woocommerce_edit_account_form_start action hook (see below).
2) After existing fields using woocommerce_edit_account_form action hook:
// Display the mobile phone field
// add_action( 'woocommerce_edit_account_form_start', 'add_billing_mobile_phone_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form', 'add_billing_mobile_phone_to_edit_account_form' ); // After existing fields
function add_billing_mobile_phone_to_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" />
</p>
<?php
}
// Check and validate the mobile phone
add_action( 'woocommerce_save_account_details_errors','billing_mobile_phone_field_validation', 20, 1 );
function billing_mobile_phone_field_validation( $args ){
if ( isset($_POST['billing_mobile_phone']) && empty($_POST['billing_mobile_phone']) )
$args->add( 'error', __( 'Please fill in your Mobile phone', 'woocommerce' ),'');
}
// Save the mobile phone value to user data
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_mobile_phone', 20, 1 );
function my_account_saving_billing_mobile_phone( $user_id ) {
if( isset($_POST['billing_mobile_phone']) && ! empty($_POST['billing_mobile_phone']) )
update_user_meta( $user_id, 'billing_mobile_phone', sanitize_text_field($_POST['billing_mobile_phone']) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
3) In a specific location, overriding myaccount/form-edit-account.php template file via the theme as explained on this documentation. and on this answer thread…
In this case you will need to add the following html code in the template (like in this answer thread):
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" />
</p>
In this last case, you will need to add in your theme's function.php file the 2 last hooked functions from section 2 (validation and saving).