Currently I have billing address set to read only using the following code but I need to add customer first & last name to the read only too. I'm placing this in my theme functions.php file
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
Add this code in function.php file
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
Related
I want it to skip product upload if the product name already exists in the database. I have such a code, but in this way the upload does not start and gives an error.
add_filter( 'wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3 );
function my_is_post_to_create( $continue_import, $data, $import_id ) {
global $product;
$product = wc_get_product(get_the_ID());
$current_title = $product->get_name();
$new_title = $data['name'];
if ( $import_id == 34 ) {
if ( $new_title == $current_title ) {
return false;
}
}
return true;
}
We have a website for orders that are then delivered. Now the thing is we want to utilize the details from the My Account Address Billing as the address to ship to, on checkout we want this populated from what's there but not allow the user to make any changes on that check out page Billing form.
This code:
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
Only makes the Address part not be changed however everything else from first name, last name, cellphone, etc is able to be modified.
We need all those fields to be locked just as that code as locked the Address field to read-only.
You can remove if($key == 'billing_address_1' || $key == 'billing_address_2'){ this line all fields. check beloe code.
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
$key_value = get_user_meta($user_id, $key, true);
if( $key_value != '' ){
if( $key == 'billing_country' || $key == 'billing_state' || $key == 'billing_suburb' ){
$checkout_fields['billing'][$key]['custom_attributes'] = array('disabled'=>'disabled');
}else{
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
For billing_country, billing_state and billing_suburb, you have to pass value in hidden because the select dropdown disabled the option value is not passed when you click place order and in order to solve this issue we can add hidden fields with our value.
add_action('woocommerce_after_order_notes', 'billing_countryand_state_hidden_field');
function billing_countryand_state_hidden_field($checkout){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
echo '<input type="hidden" class="input-hidden" name="billing_country" value="'.get_user_meta($user_id, 'billing_country', true).'">';
echo '<input type="hidden" class="input-hidden" name="billing_state" value="'.get_user_meta($user_id, 'billing_state', true).'">';
echo '<input type="hidden" class="input-hidden" name="billing_suburb" value="'.get_user_meta($user_id, 'billing_suburb', true).'">';
}
Tested and works
I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:
If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.
I believe this is what you are looking for, comment with explanation added in code
function mycustom_woocommerce_billing_fields( $fields ) {
// Get current user
$user = wp_get_current_user();
// User id
$user_id = $user->ID;
// User id is found
if ( $user_id > 0 ) {
// Fields
$read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );
// Loop
foreach ( $fields as $key => $field ) {
if( in_array( $key, $read_only_fields ) ) {
// Get key value
$key_value = get_user_meta($user_id, $key, true);
if( strlen( $key_value ) > 0 ) {
$fields[$key]['custom_attributes'] = array(
'readonly'=>'readonly'
);
}
}
}
}
return $fields;
}
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
I have created a custom column for WordPress users called "Verification". Upon WooCommerce registration, an email is sent to the user asking them to verify their email and that way, account. This information is stored in the database under usermeta (wp_usermeta) as a meta_key and that key is called is_activated.
Here is the code for the custom column:
add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
unset($column_headers['posts']);
$column_headers['account_verification'] = 'Verification Status';
return $column_headers;
}
function make_verification_status_column_sortable( $columns ) {
$columns['account_verification'] = 'Verification Status';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );
I am now trying to figure out how to fetch data from the usermeta table and in this case, the meta_key is_activated. Here is what I tried:
add_action('manage_users_custom_column', 'account_verification_information', 10, 3);
function account_verification_information($status = '1') {
global $wpdb;
$has_user_verified = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->users
LEFT JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE meta_key = 'is_activated'
AND meta_value = '$status';"
, $level ));
return $has_user_verified;
$check_verification = account_verification_information(1);
if ( $column_name === account_verification ) {
echo $check_verification;
} else {
echo '<span class="na">Not yet Verified</span>';
}
}
Hopefully, by looking at it, you "get the idea".
I need it to show "Verified" if the value is 1 and "Not Verified" if the value is 0.
I've tried so many different versions of this I am getting lost..
Any help is highly appreciated.
Try the following instead:
add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
unset($column_headers['posts']);
$column_headers['account_verification'] = __('Verification Status');
return $column_headers;
}
add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );
function make_verification_status_column_sortable( $vars ) {
$columns['account_verification'] = 'account_verification';
return $columns;
}
add_filter('manage_users_custom_column', 'add_user_column_value', 10, 3);
function add_user_column_value( $value, $column_name, $user_id ){
if ( 'account_verification' == $column_name ){
if( get_user_meta( $user_id, 'is_activated', true ) == 1 ){
$value = '<span style="color:green;font-weight:bold;">Verified</span>';
} else {
$value = '<span class="na" style="color:grey;"><em>Not Verified</em></span>';
}
}
return $value;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
I want to update the post and edit the value of an existing field with a sequence number after the entry has been created at gform_after_submission hook.
The requirement is to update a single field value in the same post and then update the post. I I found this documentation but not that helpful.
And tried the following but it's updating all the fields:
add_filter( "gform_save_field_value", "set_field_value", 10, 4 );
function set_field_value( $value, $lead, $field, $form ){
if( $form['id'] != 1 || $field['id'] != 19 )
return;
$value = "xxx";
return $value;
}
I think your logic is a bit off I would check IF it IS the right field rather than if it's not and do something like this:
add_filter( "gform_save_field_value", "nifty_set_field_value", 10, 4 );
function nifty_set_field_value( $value, $lead, $field, $form ){
//make sure we are on form 1 AND field 19
if( $form['id'] == 1 && $field['id'] == 19 ){
//set value
$value = "xxx";
//return
return $value;
}
//not our field, that's okay just return the normal value
else {return $value; }
You may be able to do it without the else and just return value once but if I remember right (and the docs show) both are needed.
Or you could do it with your current code:
add_filter( "gform_save_field_value", "nifty_set_field_value", 10, 4 );
function nifty_set_field_value( $value, $lead, $field, $form ){
if( $form['id'] != 1 || $field['id'] != 19 ) {
return $value;
}
$value = "xxx";
return $value;
}
just make sure you return the value not just return the function
Here is a link to the correct docs page Also Gravity Forms has a support form of its own for users who have paid for the product. Lastly, Check out their example pastie
I solved the issue using the below code:
add_filter("gform_get_input_value", "update_field", 10, 4);
function update_field($value, $lead, $field, $input_id){
if($lead["form_id"] == 1 && $field["id"] == 19)
return 'xxx';
else
return $value;
}
add_filter("gform_save_field_value", "save_field_value", 10, 4);
function save_field_value($value, $lead, $field, $form){
if($lead["form_id"] == 1 && $field["id"] == 19)
return 'xxx';
else
return $value;
}