I set up a simple checkbox field in the user account admin interface. Here is how I am displaying/saving it:
function show_free_ground_field( $user ) {
?>
<h3>Free Ground Shipping</h3>
<table class="form-table">
<tr>
<th>Free ground for order > $1000</th>
<td>
<?php
woocommerce_form_field( 'freeGround', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes'),
), '' );
?>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'show_free_ground_field' );
add_action( 'edit_user_profile', 'show_free_ground_field' );
function save_free_ground_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ){
return false;
}
if ( ! empty( $_POST['freeGround'] ) ){
update_usermeta( $user_id, 'freeGround', $_POST['freeGround'] );
}
}
add_action( 'personal_options_update', 'save_free_ground_field' );
add_action( 'edit_user_profile_update', 'save_free_ground_field' );
It displays fine, but if I check it off and re-visit the same user after saving the checkbox is unchecked. How do I fix that?
You should need to get the saved value for this checkbox field in the first function:
add_action( 'show_user_profile', 'show_free_ground_field' );
add_action( 'edit_user_profile', 'show_free_ground_field' );
function show_free_ground_field( $user ) {
?>
<h3>Free Ground Shipping</h3>
<table class="form-table">
<tr>
<th>Free ground for order > $1000</th>
<td>
<?php
$freeGround = get_user_meta( $user->id, 'freeGround', true );
if ( empty( $freeGround ) ) $freeGround = '';
woocommerce_form_field( 'freeGround', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes'),
), $freeGround );
?>
</td>
</tr>
</table>
<?php
}
This should work now
Related
I have added to admin users a custom meta field using the following code:``
function wporg_usermeta_form_field_birthday( $user )
{
?>
<table class="form-table" id="table-form-dob" >
<tr>
<th><h3 style="margin: 0">Extra Meta Fields</h3></th>
</tr>
<tr>
<th>
<label for="user_dob">Birthday</label>
</th>
<td>
<input type="date"
class="regular-text ltr"
id="user_dob"
name="user_dob"
value="<?= esc_attr( get_user_meta( $user->ID, 'user_dob', true ) ) ?>"
title="Please use YYYY-MM-DD as the date format."
pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
required>
</td>
</tr>
</table>
<script>
jQuery(function($){
jQuery('#table-form-dob tr').insertAfter(jQuery('#display_name').parentsUntil('tr').parent());
});
</script>
<?php
}
function wporg_usermeta_form_field_birthday_update( $user_id )
{
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
return update_user_meta(
$user_id,
'user_dob',
$_POST['user_dob']
);
}
add_action(
'show_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'edit_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'personal_options_update',
'wporg_usermeta_form_field_birthday_update'
);
add_action(
'edit_user_profile_update',
'wporg_usermeta_form_field_birthday_update'
);
register_meta('user', 'user_dob', array(
"type" => "string",
"show_in_rest" => true // this is the key part
));
I want to add this same field In woocommerce checkout page, so when the user get registered in woocommerce checkout page, we should be able to see this "Birthday" field (user_dob) in admin user profile/ edit section.
also, I am accessing user meta in rest API currently its showing meta in rest API after user saver values in check it should value in wp rest API.
How can I add this?
You can use the following that will add user_dob custom field to checkout account registration fields:
add_filter( 'woocommerce_checkout_fields', 'add_checkout_account_birthday_field' );
function add_checkout_account_birthday_field( $fields ){
$fields['account']['user_dob'] = array(
'type' => 'date',
'label' => __("Birthday", "woocommerce"),
'placeholder' => __("Please use YYYY-MM-DD as the date format.", "woocommerce"),
'class' => array('form-row-wide regular-text ltr'),
'required' => true,
'custom_attributes' => ['pattern' => '(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])'],
);
return $fields;
}
add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
function save_checkout_account_birthday_field( $customer, $data ){
if ( isset($_POST['user_dob']) && ! empty($_POST['user_dob']) ) {
$customer->update_meta_data( 'user_dob', sanitize_text_field($_POST['user_dob']) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Once order is placed the birth date is saved to user data and displayed in admin user "Birthday" field.
I have tax enabled for my WooCommerce installation. It calculates tax for all customer's who live in my state (based on their shipping address). This is the default setup for WooCommerce.
Some customers are tax exempt and shouldn't be charged tax. I created a custom field in the user profile where I can check a box to exempt customers from being charged tax. This works correctly.
I tried to find a hook where I can use that selection to disable the tax but the code I have causes the checkout page to be blank for all users. No error message is displayed.
My functions.php code is as follows:
///////////////////////////////////////
/* Tax exempt customers */
///////////////////////////////////////
// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
?>
<h3><?php _e("Tax status"); ?></h3>
<table class="form-table">
<tr>
<th><?php _e("Tax exempt"); ?></th>
<td>
<?php
woocommerce_form_field( 'tax_exempt', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Allowed'),
), get_user_meta( $user->id, 'tax_exempt', true ) );
?>
</td>
</tr>
</table>
<?php
}
// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
}
}
// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_tax_class', 'disable_tax_calculation' );
function disable_tax_calculation( $tax_class, $product ) {
if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
///////////////////////////////////////
/* END: Tax exempt customers */
///////////////////////////////////////
Since Woocommerce 3 woocommerce_product_tax_class hook is deprecated and has been replaced. I have updated your 3rd function below:
// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
?>
<h3><?php _e("Tax status"); ?></h3>
<table class="form-table">
<tr>
<th><?php _e("Tax exempt"); ?></th>
<td>
<?php
woocommerce_form_field( 'tax_exempt', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Allowed'),
), get_user_meta( $user->id, 'tax_exempt', true ) );
?>
</td>
</tr>
</table>
<?php
}
// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
}
}
// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_get_tax_class', 'disable_tax_calculation', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'disable_tax_calculation', 10, 2 );
function disable_tax_calculation( $tax_class, $product ) {
if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
Code goes in functions.php file of your active child theme (or active theme). It should better work.
Related: Disable tax programmatically for a specific user role
I have added to admin users a custom meta field using the following code:``
function wporg_usermeta_form_field_birthday( $user )
{
?>
<table class="form-table" id="table-form-dob" >
<tr>
<th><h3 style="margin: 0">Extra Meta Fields</h3></th>
</tr>
<tr>
<th>
<label for="user_dob">Birthday</label>
</th>
<td>
<input type="date"
class="regular-text ltr"
id="user_dob"
name="user_dob"
value="<?= esc_attr( get_user_meta( $user->ID, 'user_dob', true ) ) ?>"
title="Please use YYYY-MM-DD as the date format."
pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
required>
</td>
</tr>
</table>
<script>
jQuery(function($){
jQuery('#table-form-dob tr').insertAfter(jQuery('#display_name').parentsUntil('tr').parent());
});
</script>
<?php
}
function wporg_usermeta_form_field_birthday_update( $user_id )
{
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
return update_user_meta(
$user_id,
'user_dob',
$_POST['user_dob']
);
}
add_action(
'show_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'edit_user_profile',
'wporg_usermeta_form_field_birthday'
);
add_action(
'personal_options_update',
'wporg_usermeta_form_field_birthday_update'
);
add_action(
'edit_user_profile_update',
'wporg_usermeta_form_field_birthday_update'
);
register_meta('user', 'user_dob', array(
"type" => "string",
"show_in_rest" => true // this is the key part
));
I want to add this same field In woocommerce checkout page, so when the user get registered in woocommerce checkout page, we should be able to see this "Birthday" field (user_dob) in admin user profile/ edit section.
also, I am accessing user meta in rest API currently its showing meta in rest API after user saver values in check it should value in wp rest API.
How can I add this?
You can use the following that will add user_dob custom field to checkout account registration fields:
add_filter( 'woocommerce_checkout_fields', 'add_checkout_account_birthday_field' );
function add_checkout_account_birthday_field( $fields ){
$fields['account']['user_dob'] = array(
'type' => 'date',
'label' => __("Birthday", "woocommerce"),
'placeholder' => __("Please use YYYY-MM-DD as the date format.", "woocommerce"),
'class' => array('form-row-wide regular-text ltr'),
'required' => true,
'custom_attributes' => ['pattern' => '(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])'],
);
return $fields;
}
add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
function save_checkout_account_birthday_field( $customer, $data ){
if ( isset($_POST['user_dob']) && ! empty($_POST['user_dob']) ) {
$customer->update_meta_data( 'user_dob', sanitize_text_field($_POST['user_dob']) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Once order is placed the birth date is saved to user data and displayed in admin user "Birthday" field.
I have added the code below. The birthday field is showing in my account page and also in WP admin user page as well but the problem is that the date is not saving.
What I have so far
function iconic_get_account_fields() {
return apply_filters( 'iconic_account_fields', array(
'user_url' => array(
'type' => 'date',
'label' => __( 'My Birth Date', 'iconic' ),
'placeholder' => __( 'Date of Birth', 'iconic' ),
'required' => true,
),
) );
}
/**
* Add fields to registration form and account area.
*/
function iconic_print_user_frontend_fields() {
$fields = iconic_get_account_fields();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_edit_account_form', 'iconic_print_user_frontend_fields', 10 ); // my account
/**
* Add fields to admin area.
*/
function iconic_print_user_admin_fields() {
$fields = iconic_get_account_fields();
?>
<h2><?php _e( 'Additional Information', 'iconic' ); ?></h2>
<table class="form-table" id="iconic-additional-information">
<tbody>
<?php foreach ( $fields as $key => $field_args ) { ?>
<tr>
<th>
<label for="<?php echo $key; ?>"><?php echo $field_args['label']; ?></label>
</th>
<td>
<?php $field_args['label'] = false; ?>
<?php woocommerce_form_field( $key, $field_args ); ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
add_action( 'show_user_profile', 'iconic_print_user_admin_fields', 30 ); // admin: edit profile
add_action( 'edit_user_profile', 'iconic_print_user_admin_fields', 30 ); // admin: edit other users
I use the code partly from:
The Ultimate Guide to Adding Custom WooCommerce Registration Fields
The following code will add (and save) a custom birthday field to
My account - edit account
Admin user page - profile
// Add field - my account
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'My Birth Date', 'woocommerce' ),
'placeholder' => __( 'Date of Birth', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Please provide a birth date', '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['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add field - admin
function add_user_birtday_field( $user ) {
?>
<h3><?php _e('Birthday','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="birthday_field"><?php _e( 'Date of Birth', 'woocommerce' ); ?></label></th>
<td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
// Save field - admin
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
I want to add otp when user register on woo-commerce registration page, i have added custom field for phone number but i am not able to get any sources for otp in word-press i have my own API please if someone can share any resource
for finding it
It will be very helpful ...
function iconic_get_account_fields() {
return apply_filters( 'iconic_account_fields', array(
'user_url' => array(
'type' => 'text',
'label' => __( 'Phone' ),
'placeholder' => __( 'E.g. 9861234567', 'iconic' ),
'required' => true,
),
) );
}
function iconic_print_user_frontend_fields() {
$fields = iconic_get_account_fields();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_register_form', 'iconic_print_user_frontend_fields', 1 );
add_action( 'woocommerce_edit_account_form', 'iconic_print_user_frontend_fields', 10 ); // my account
function iconic_print_user_admin_fields() {
$fields = iconic_get_account_fields();
?>
<h2><?php _e( 'Additional Information', 'iconic' ); ?></h2>
<table class="form-table" id="iconic-additional-information">
<tbody>
<?php foreach ( $fields as $key => $field_args ) { ?>
<tr>
<th>
<label for="<?php echo $key; ?>"><?php echo $field_args['label']; ?></label>
</th>
<td>
<?php $field_args['label'] = false; ?>
<?php woocommerce_form_field( $key, $field_args ); ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
add_action( 'show_user_profile', 'iconic_print_user_admin_fields', 30 ); // admin: edit profile
add_action( 'edit_user_profile', 'iconic_print_user_admin_fields', 30 );
.