Make My account edit-account fields readonly in Woocommerce - php

I need to make woocommerce my-account/edit-account/ page email, or last name or another field. One field or two fields, ReadOnly.
My solution is edit form-edit-account.php file in plugins/woocommerce/templates/myaccount folder and edit form-edit-account.php file and add readonly to end of tags and work correctly. For example:
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" readonly />
</p>
But I need make this change only from function.php file. Is this possible? I need code for functions.php file.

You could also use jQuery instead (here for the "billing_mobile_phone" field):
add_action( 'wp_footer' , 'make_phone_field_readonly' );
function make_phone_field_readonly(){
// Only for account fields
if( is_account_page() ): ?>
<script type='text/javascript'>
jQuery(function($){
$('form.edit-account input#billing_mobile_phone').prop('readonly', true );
});
</script>
<?php endif;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or as in your penultimate question, you will use the hooked function adding readonly attribute to your additional fields:
// 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 ); ?>" readonly />
</p>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So at all you have 3 alternatives:
Add the readonly attribute with jQuery (may be the best way)
Overriding the template via the theme (adding the readonly attribute)
Add custom account fields with the read only attribute

Related

How to add a custom nickname field on WooCommerce My Account's edit page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have customized the myaccount/form-edit-account.php WooCommerce template file. So that a customer in addition to the default fields first name, last name, display name, email and password. Now also has the option to add/edit his nickname.
Once logged in, he can access and edit that information on his account page.
Here the code I have added in the myaccount/form-edit-account.php on line 35 (between the last name and the display name field.
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-wide">
<label for="account_nickname"><?php esc_html_e( 'Téléphone (non modifiable. Si erreur contacer Laura au 07.66.89.85.05) ', 'woocommerce' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_nickname" id="account_nickname" autocomplete="family-name" value="<?php echo esc_attr( $user->nickname ); ?>" />
</p>
<div class="clear"></div>
Unfortunatly, when he wants to modify the nickname, and clicks on the submit button, no error occurs, but the nickname is not updated.
Someone who can tell me what further adjustments are needed? am I doing something wrong somewhere?
First of all, instead of using your custom code, use following code to add the field to the myaccount/form-edit-account.php template file (On line 35, or elsewhere if desired, but in any case between existing fields.
Note: This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-account.php.
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_nick_name"><?php esc_html_e( 'Nickname', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_nick_name" id="account_nick_name" value="<?php echo get_user_meta( get_current_user_id(), 'nickname', true ); ?>"/>
</p>
<div class="clear"></div>
Now you will see that the field has been added BUT the code to validate/save the value of the field, you must also add yourself.
So for validation and saving add the following code to functions.php (or the way you prefer to add snippets)
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset( $_POST['account_nick_name'] ) && empty( $_POST['account_nick_name'] ) ) {
$args->add( 'error', __( 'Please provide a nick name', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset( $_POST['account_nick_name'] ) && ! empty( $_POST['account_nick_name'] ) ) {
update_user_meta( $user_id, 'nickname', sanitize_text_field( $_POST['account_nick_name'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );

Add and save a text field in WooCommerce customer order detail pages

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.

Customize WooCommerce login form user fields

I would like to include a placeholder in the woocommerce login form.
Looking at this for a starting point:
woocommerce add placeholder text to edit account form:
add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) {
if ( $args['id'] == 'username' ) {
$args['placeholder'] = 'My placeholder text';
}
return $args;
};
Not working as it is. Open to any suggestions.
As the login fields are hard coded in myaccount/form-login.php template file, the only way to add placeholder(s) to those fields, requires overriding the related template via the active child theme (or active theme)
Once you have copied the myaccount/form-login.php template file located in WooCommerce plugin under the "templates" folder to (as explained on the template):
<?php
/**
* Login Form
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-login.php.
Open/Edit form-login.php copied file, and add to all desired <input> html tags:
placeholder="placeholder text"
Like for example replacing from line 38 to 45 with the following:
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="username"><?php esc_html_e( 'Username or email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" placeholder="<?ph esc_html_e("Here type your Username (or email address)"); ?>" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="password" id="password" placeholder="<?ph esc_html_e(""Here type your password"); ?>" autocomplete="current-password" />
</p>
Then save… You will get something like:
Related threads:
WooCommerce condition for is_account_page(), but only the login portion
Add a field to Woocommerce registration form and in admin edit user
Sync additional Billing registration fields with default Wordpress fields in WooCommerce

Add a mobile phone field on My account > edit account in Woocommerce

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).

ask without log in or entering email in DWQA (wordpress plugin)

IN QW question & Answer ( wordpress plugin ) ,how can user add question without registering(log in) or entering their email address ?
I want that all user(guest) can be able to ask their questions without entering their email address or even registering.
I had the same issue and I just resolved it.
DWQA version 1.3.3
You have to replace the function
function dwqa_require_field_submit_question()
with your own version. You find the old function at line 79 in your template-functions.php in the plugin folder.
Just removing the function from the queue will make it impossible to add new questions. So you just add the adjusted version of the function to your functions.php in your theme folder and and remove the standard function and add the adjusted function.
Add following to your functions.php:
function dwqa_require_field_submit_question_adjusted(){
?>
<input type="hidden" name="dwqa-action" value="dwqa-submit-question" />
<?php wp_nonce_field( 'dwqa-submit-question-nonce-#!' ); ?>
<?php
$subscriber = get_role( 'subscriber' );
?>
<?php if ( ! is_user_logged_in() && ! dwqa_current_user_can( 'post_question' ) ) { ?>
<input type="hidden" name="login-type" id="login-type" value="sign-up" autocomplete="off">
<div class="question-register clearfix">
<label for="user-email"><?php _e( 'You need an account to submit question and get answers. Create one:','dwqa' ) ?></label>
<div class="register-email register-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your email','dwqa' ) ?>" name="user-email">
</div>
<div class="register-username register-input">
<input type="text" size="20" value="" class="input" placeholder="Choose an username" name="user-name-signup" id="user-name-signup">
</div>
<div class="login-switch"><?php _e( 'Already a member?','dwqa' ) ?> <a class="credential-form-toggle" href="<?php echo wp_login_url(); ?>"><?php _e( 'Log In','dwqa' ) ?></a></div>
</div>
<div class="question-login clearfix dwqa-hide">
<label for="user-name"><?php _e( 'Login to submit your question','dwqa' ) ?></label>
<div class="login-username login-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your username','dwqa' ) ?>" id="user-name" name="user-name">
</div>
<div class="login-password login-input">
<input type="password" size="20" value="" class="input" placeholder="<?php _e( 'Type your password','dwqa' ) ?>" id="user-password" name="user-password">
</div>
<div class="login-switch"><?php _e( 'Not yet a member?','dwqa' ) ?> <a class="credential-form-toggle" href="javascript:void( 0 );" title="<?php _e( 'Register','dwqa' ) ?>"><?php _e( 'Register','dwqa' ) ?></a></div>
</div>
<?php } else if ( ! is_user_logged_in() && dwqa_current_user_can( 'post_question' ) ) { ?>
//Here was the old code which had shown the E-mail input and login information
<?php }
}
This function will replace the old function
function dwqa_require_field_submit_question()
Now just remove the old function and add the new to the queue in your functions.php :)
remove_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question' );
add_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question_adjusted' );
That is it :). Maybe not the best way, but it avoids changing the plugin code.
Have a nice day
//Removes BuddyBar from non-admins only
function splen_remove_admin_bar() { if( !is_super_admin() )
add_filter( ‘show_admin_bar’, ‘__return_false’ );
}
add_action(‘wp’, ‘splen_remove_admin_bar’);

Categories