I tried to implement custom field into WooCommerce registration page on my site, and works great, but if want to Modify that date into My Account page, changes are not saved. Looks like i have missed something somewhere that don't proceed data to database. This is code for custom field:
<p class="form-row form-row-last">
<label for="reg_billing_birthdate"><?php _e( 'Date of Birth', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_birthdate" id="reg_billing_birthdate" value="<?php if ( ! empty( $_POST['billing_birthdate'] ) ) esc_attr_e( $_POST['billing_birthdate'] ); ?>" />
</p>
column billing_birthdate is created into wp_usermeta table, But i added this code also into functions.php into theme:
and this code to proceed data to database..
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_birthdate'] ) ) {
// Billing Address field which is by default
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_birthdate'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_birthdate', sanitize_text_field( $_POST['billing_birthdate'] ) );
}
}
I alse added this code to functions.php file, to be able to edit that field into edit-account page.
/**
* To display additional field at My Account page
* Once member login: edit account
*/
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
function my_woocommerce_edit_account_form() {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( !$user )
return;
$birthdate = get_user_meta( $user_id, 'birthdate', true );
?>
<fieldset>
<p class="form-row form-row-thirds">
<label for="birthdate">Birth date:</label>
<input type="text" name="birthdate" value="<?php echo esc_attr( $birthdate ); ?>" class="input-text" />
<br />
<span style="font-size: 12px;">(Birth date format: DD-MM-YYYY. eg: 31-12-2005)</span>
</p>
</fieldset>
<?php
} // end func
/**
* This is to save user input into database
* hook: woocommerce_save_account_details
*/
add_action( 'woocommerce_save_account_details', 'my_woocommerce_save_account_details' );
function my_woocommerce_save_account_details( $user_id ) {
update_user_meta( $user_id, 'birthdate', htmlentities( $_POST[ 'birthdate' ] ) );
} // end func
So my question is where is my error my after editing that field into my-account page, changes are not reflected into account dashboard.
What I found that, you are updating the field called 'billing_birthdate' at the time you are inserting the data. But when you are editing you are updating the field 'birthdate'.
For update :
update_user_meta( $customer_id, 'billing_birthdate',htmlentities( $_POST['birthdate'] ) );
Hope this will work for you.
Related
I want to create a field where the customer can write the reason for canceling a created order. The code below appears in other orders as well, due to the customer's own knowledge. I made several attempts to register the order, but I could not get the result I wanted. I thought about "update_post_meta" and "order_id" but I guess I failed. Also, I couldn't decide where it would make sense for this area to appear on the admin page. Thanks.
// 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 source: https://stackoverflow.com/a/62777930/14597323
For new users: A checkbox has been added to the Woocommerce Registration form. Once user checks that checkbox it needs to display checked in the WordPress Profile of that user as well as the account details tabs (Above the save details button).
For Current users: The checkbox is unchecked in their WordPress Profiles as well as their Woocommerce account details tab (Above the save details button).
Currently, I am able to check the checkbox upon registering, which then shows that it is checked in my WordPress Profile, however not checked on my Woocommerce Account Details Tab.
Hope this makes more sense. 😶
Here is the code I am using below:
// user consent on registration
//
// Add the checkbox to registration form
add_action( 'register_form', 'foo_add_privacy_policy_field' );
function foo_add_privacy_policy_field() {
?>
<p>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" />
<label for="foo_privacy_policy">
<?php _e( 'Privacy Policy', 'foo' ) ?>
</label>
</p>
<?php
}
// Validate the checkbox value in the registration form so that it is required
add_filter( 'registration_errors', 'foo_privacy_policy_auth', 10, 3 );
function foo_privacy_policy_auth( $errors, $sanitized_user_login, $user_email ) {
if ( !isset( $_POST[ 'foo_privacy_policy' ] ) ):
$errors->add( 'policy_error', "<strong>ERROR</strong>: Please accept the privacy policy." );
return $errors;
endif;
return $errors;
}
// Fill the meta 'foo_privacy_policy' with the value of the checkbox
add_action( 'personal_options_update', 'foo_privacy_policy_save' );
add_action( 'edit_user_profile_update', 'foo_privacy_policy_save' );
add_action( 'woocommerce_save_account_details', 'foo_privacy_policy_save' );
add_action( 'user_register', 'foo_privacy_policy_save' );
function foo_privacy_policy_save( $user_id ) {
if ( isset( $_POST[ 'foo_privacy_policy' ] ) ) {
update_user_meta( $user_id, 'foo_privacy_policy', $_POST[ 'foo_privacy_policy' ] );
} else {
update_user_meta( $user_id, 'foo_privacy_policy', 'off' );
}
}
// Add the checkbox to user profile home
add_action( 'woocommerce_register_form', 'foo_show_extra_profile_fields' );
add_action( 'woocommerce_edit_account_form', 'foo_show_extra_profile_fields' );
add_action( 'show_user_profile', 'foo_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'foo_show_extra_profile_fields' );
function foo_show_extra_profile_fields( $user ) {
?>
<h3>
<?php esc_html_e( 'Privacy Policy', 'foo' ); ?>
</h3>
<table class="form-table">
<tr>
<td>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" value='1' style="float: left; height: 50px;" <?php if (esc_attr(get_the_author_meta('foo_privacy_policy', $user->ID))=='on' ){ echo "checked"; } ?> />
<label for="foo_privacy_policy">
<?php _e( 'Privacy Policy', 'foo' ) ?>
</label>
</td>
</tr>
</table>
<?php
}
I want to change some functions in woocommerce account-edit page. I want user_email or account_email fields equals to billing_email.
I displayed a billing_email field in Woocommerce Edit Account Page and then try to update_user_metaa but its not working.
// add_action( 'woocommerce_edit_account_form_start', 'add_billing_email_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form_start', 'add_billing_details_to_edit_account_form' );
function add_billing_details_to_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="billing_email"><?php _e( 'Email Address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="billing_email" id="billing_email" value="<?php echo esc_attr( $user->billing_email ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_user', 20, 1 );
function my_account_saving_billing_user( $user_id ) {
if( isset($_POST['billing_email']) && ! empty($_POST['billing_email']) )
update_user_meta( $user_id, 'account_email', sanitize_text_field($_POST['billing_email']) );
}
I want when user update his billing_email from Woocommerce Edit Account page then user_email or account_email is also changed.
Both billing_email and user_email or account_email must be same.
add this to your functions.php and when you change the account in one place or the other both will change, so both places will be the same. If you are missing any information let me know:
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_user', 20, 1 );
function my_account_saving_billing_user( $user_id ) {
if( isset( $_POST['account_email'] ) && $_POST['account_email'] != '' ){
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
}
}
add_action( 'woocommerce_customer_save_address', 'save_billing_email_to_user', 12, 1 );
function save_billing_email_to_user( $user_id ) {
if( isset( $_POST['billing_email'] ) && $_POST['billing_email'] != '' ){
$args = array(
'ID' => $user_id,
'user_email' => sanitize_text_field( esc_attr( $_POST['billing_email'] ))
);
wp_update_user( $args );
}
}
How do i add value to a custom text field in user profile when an hook is triggered/fired. I have been able to add a custom field called example using the following code
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><?php _e("Example Section", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="example"><?php _e("Example"); ?></label></th>
<td>
<input type="text" name="example" id="example" value="<?php echo esc_attr( get_the_author_meta( 'example', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("This field should add YES when to field when a hook is triggered ad empty if hook not triggered."); ?></span>
</td>
</tr>
</table>
<?php }
And save the input using the following code
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;
}
update_user_meta( $user_id, 'example', $_POST['example'] );
}
i would like this newly generated custom field to be populated with YES when the user_register hook is triggered (i.e. when a user registers, YES should be added to the field and updated)
So that i can get the YES value and use it to display content dynamically. Something like
$user = wp_get_current_user();
if ( get_the_author_meta( 'example', $user->ID ) = 'YES') {
//Show this page
} else {
return 'Your are not allowed to view this page';
}
How can i achieve this? Thanks
This should work:
function add_yes_to_field ( $user_id ) {
update_user_meta( $user_id, 'winner', 'YES' );
}
add_action( 'user_register', 'add_yes_to_field');
Regarding get_the_author_meta(), I don't think you can actually get user meta data with it, because it calls get_userdata(). You can use get_user_meta() instead.
https://developer.wordpress.org/reference/functions/get_the_author_meta/
I'm working on a custom plugin for a client and I need to add some custom user fields. I've searched through the Codex but couldn't come across the answer.
I basically need to add some new rows to the users table in the MySQL and then add some extra fields during the registration. I'm sure there are other plugins out there that allow you to add custom user fields but I'd like to incorporate it directly into my plugin.
How can I do it?
I've answered a similar Question at WordPress Answers: Checkboxes in registration form.
You need the action hooks register_form (to inject your input fields) and user_register (to process it). The rest of the code is just sample code to check the results in the pages Profile and User Edit.
// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );
// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
function signup_fields_wpse_87261() {
?>
<label>
<input type="checkbox" name="custom_feature_a" id="custom_feature_a" />
Enable feature A?
</label>
<br />
<label>
<input type="checkbox" name="custom_feature_b" id="custom_feature_b" />
Enable feature B?
</label>
<hr />
<?php
}
function handle_signup_wpse_87261( $user_id, $data = null )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
if ( $feat_a )
{
add_user_meta( $user_id, 'custom_feature_a', $feat_a );
}
if ( $feat_b )
{
add_user_meta( $user_id, 'custom_feature_b', $feat_b );
}
}
function user_field_wpse_87261( $user )
{
$feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
$feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
<h3><?php _e('Custom Fields'); ?></h3>
<table class="form-table">
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
checked( $feat_a, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature A?'); ?></span>
</label>
</td>
</tr>
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
checked( $feat_b, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature B?'); ?></span>
</label>
</td>
</tr>
</table>
<?php
}
function save_profile_fields_87261( $user_id )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
update_usermeta( $user_id, 'custom_feature_a', $feat_a );
update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}