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.
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 would like to synhcronize a custom checkbox field in WooCommerce checkout page with a similar one in the My Account page (when the user is logged in).
Here's my code:
// Show a checkbox in checkout page and in My account > Details
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
add_action( 'woocommerce_after_order_notes', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe to my newsletter', 'woocommerce' ),
), get_user_meta(get_current_user_id(), 'newsletter-account', true ) );
}
// Save checkbox value when saved in My account > Details
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_from_account_details', 10, 1 );
function save_checkbox_value_from_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
Those two blocks of code work fine: if in My account > Details I checked the checkbox, the preference is saved and I can see a checked checkbox also in the checkout page.
Now the problem is that I need a way to obtain the same result when I edit my newsletter preference from the checkout page. I think I need to use the woocommerce_checkout_create_order but I haven't any idea on how to code this:
//Save the checkbox value when customer edit it from the checkout page
add_action( 'woocommerce_checkout_create_order', 'save_checkbox_value_from_checkout_page'. 10, 1 );
function save_checkbox_value_from_checkout_page() {
//some code here
}
Any suggestions are appreciated.
Updated
The hook woocommerce_checkout_create_order is for order meta data and what you need is to save/update your checkout newsletter-account field value as user meta data…
So is better to use dedicated woocommerce_checkout_update_customer hook.
Here is your complete code (tested and working):
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
add_action( 'woocommerce_after_order_notes', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe to my newsletter', 'woocommerce' ),
), get_user_meta( get_current_user_id(), 'newsletter-account', true ) );
}
// Save/update checkbox value when saved in My account > Details
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_from_account_details', 10, 1 );
function save_checkbox_value_from_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
// Save/update custom checkout field value as user meta data
add_action('woocommerce_checkout_update_customer','custom_checkout_checkbox_update_customer', 100, 2 );
function custom_checkout_checkbox_update_customer( $customer, $data ){
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $customer->get_id(), 'newsletter-account', $value ); // Updated
}
Code goes in functions.php file of your active child theme (or active theme).
Now if you want to grab that information additionally to the order as order meta data you will use:
add_action('woocommerce_checkout_create_order','custom_checkout_checkbox_add_order_meta', 100, 2 );
function custom_checkout_checkbox_add_order_meta( $order, $data ){
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
$order->update_meta_data( 'newsletter-account', $value );
}
In the Checkout form I created a select field. My question is how in Wordpress or Woocmmerce this camp can be left as required.
<p class="form-row form-row-wide validate-required validate-region" id="shipping_region_field" data-priority="6">
<select name="shipping_region" id="shipping_region" class="state_select select2-selection--single" autocomplete="address-level1" data-placeholder="" tabindex="-1" aria-hidden="true">
<option>Opción 01</option>
<option>Opción 02</option>
</select>
</p>
1) For normal or custom billing and shipping fields you can use woocommerce_billing_fields or woocommerce_shipping_fields action hooks on checkout page as follow.
It will make a custom checkout field required without any need to add a validation script and to save it in the order. The field will also appear in My account edit adresses field section.
Some argument explanations:
The class 'update_totals_on_change' allow to trigger "update checkout" on change.
The 'required' attribute make the field required or not
The 'priority' attribute allow you to change the location of the field.
The code:
add_filter( 'woocommerce_shipping_fields', 'display_shipping_region_checkout_field', 20, 1 );
function display_shipping_region_checkout_field( $fields ) {
$fields['shipping_region'] = array(
'type' => 'select',
'label' => __("Region", "woocommerce") ,
'class' => array('form-row-wide', 'update_totals_on_change'),
'required' => true,
'options' => array(
'' => __("Choose a region please"),
'option-1' => __("Option 01"),
'option-2' => __("Option 02"),
'option-3' => __("Option 03"),
),
'priority' => 100,
'clear' => true,
);
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) In specific cases you need to use the woocommerce_default_address_fields filter. This filter is applied to all billing and shipping default fields *(see the documentation). It's used only some default checkout fields.
3) You can also use woocommerce_checkout_fieldsthat has $fields as function argument (see documentation).
4) For other custom checkout fields you can use one of the following hooks with the woocommerce_form_field() function:
woocommerce_before_checkout_billing_form has $checkout as function argument
woocommerce_before_checkout_billing_form has $checkout as function argument
woocommerce_before_checkout_shipping_form has $checkout as function argument
woocommerce_before_checkout_shipping_form has $checkout as function argument
woocommerce_before_order_notes has $checkout as function argument
woocommerce_after_order_notes has $checkout as function argument
The code that display the field, validate the field and save the field in the order:
// Display field
add_action( 'woocommerce_after_checkout_shipping_form', 'display_shipping_region_after_checkout_shipping_form', 10, 1 );
function display_shipping_region_after_checkout_shipping_form ( $checkout ) {
woocommerce_form_field( 'shipping_region', array(
'type' => 'select',
'label' => __("Region", "woocommerce") ,
'class' => array('form-row-wide','update_totals_on_change'),
'required' => true,
'options' => array(
'' => __("Choose a region please"),
'option-1' => __("Option 01"),
'option-2' => __("Option 02"),
'option-3' => __("Option 03"),
),
'priority' => 100,
'clear' => true,
), $checkout->get_value( 'shipping_region' ) );
}
// Field Validation
add_action('woocommerce_checkout_process', 'shipping_region_custom_checkout_field_validation');
function shipping_region_custom_checkout_field_validation() {
if ( isset($_POST['shipping_region']) && empty($_POST['shipping_region']) )
wc_add_notice( __( 'Please select something into Region field.' ), 'error' );
}
// Save Field value
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['shipping_region']) && empty($_POST['shipping_region']) ) {
$order->update_meta_data( '_shipping_region', esc_attr($_POST['shipping_region']) );
if( $order->get_user_id() > 0 )
update_user_met( $order->get_user_id(), 'shipping_region', esc_attr($_POST['shipping_region']) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
WooCommerce Documentation: Customizing checkout fields using actions and filters
you can add an attribute to the select tag
<select required name="shipping_region" id="shipping_region" class="state_select select2-selection--single" autocomplete="address-level1" data-placeholder="" tabindex="-1" aria-hidden="true">
<option>Opción 01</option>
<option>Opción 02</option>
</select>
**If You Have Created Your Custom Field at the checkout page**
add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
// if any validation errors
if ( empty( $_POST['woo_shipping_time'] ) ) {
$errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
}
}
**
If You Have Created Your Custom Field at the checkout page
**
add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
// if any validation errors
if ( empty( $_POST['woo_shipping_time'] ) ) {
$errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
}
}
I added a custom field on the woocommerce checkout page that is populated by URL but I am struggling to find a way to make this field not editable.
I added the following code to function.php:
//Add custom field
function custom_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['imei'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('IMEI'),
'placeholder' => __('imei'),
'default' => $_GET['imei'],
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields' );
What should I change in my code to accomplish this ?
Try this code. It work for me. Just need to add custom attribute parameter to the fields array.
//Display custom field
function custom_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['imei'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('IMEI'),
'placeholder' => __('imei'),
'default' => $_GET['imei'],
'custom_attributes' => array( 'disabled' => true)
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields' );
// Save custom field
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['imei'] ) ) {
update_post_meta( $order_id, '_imei', sanitize_text_field( $_POST['imei'] ) );
}
}
You should try this text <imput> field with the readonly property (meaning not editable).
You should need to have your 'emei' set in the checkout url like:
http://www.example.com/checkout/?imei=3545454653 to make the field appear with the value as I have this condition: if( empty($_GET['imei'])) return;set in the function.
The code:
// Display
add_action( 'woocommerce_after_order_notes', 'custom_woocommerce_checkout_fields' );
function custom_woocommerce_checkout_fields( $checkout ) {
// Only display field if the 'emei' is set in the checkout url
if( empty($_GET['imei'])) return;
echo '<p class="form-row my-field-class form-row-wide woocommerce-validated" id="imei_field">
<label for="imei" class="">'.__('IMEI').'</label>
<input type="text" class="input-text " name="imei" id="imei" placeholder="'.__('IMEI').'" value="'.$_GET['imei'].'" readonly>
</p>';
}
// Save
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['imei'] ) ) {
update_post_meta( $order_id, '_imei', sanitize_text_field( $_POST['imei'] ) );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and work on WooCommerce versions 3+
To get the value (for a defined $order_id):
$imei = get_post_meta( $order_id, '_imei', true );