Woocommerce Readonly Billing Fields - wordpress

I have some e-commerce website where the customer billing address is predefined on the back-end.
I need to set the "Billing Address" fields as 'readonly' to avoid the customer to replace the information placed there... but i donĀ“t know how/where to do it...
Is it possible?

Put following code in your theme's "function.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;
}
This function checks if the address fields have value (i.e. if the address is specified), and if it has value, makes the field/s readonly. Else keeps the fields open to add data for user.
Hope this helps.

You have not specified which form you want to customize making the billing address fields read-only. Normally the billing address fields appear on two types of forms on a WooCommerce site:
On a checkout form
On a my-account/edit-address/billing/ page
If your case is the first one, then zipkundan's answer is the best one. But if your case is the second one, then copy and paste the following code to your active theme's (or child theme if any) functions.php file:
add_filter('woocommerce_address_to_edit', 'cb_woocommerce_address_to_edit');
function cb_woocommerce_address_to_edit($address){
array_key_exists('billing_first_name', $address)?$address['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_last_name', $address)?$address['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_email', $address)?$address['billing_email']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_email-2', $address)?$address['billing_email-2']['custom_attributes'] = array('readonly'=>'readonly'):'';
return $address;
}
The above code will make the following fields read-only:
Billing first name
Billing last name
Billing email address
Billing confirm email address
Array keys for other form fields on the same page are as follows:
billing_company
billing_country
billing_address_1
billing_address_2
billing_city
billing_state
billing_postcode
billing_phone
Additionally, you can make the read-only fields appear slightly faded out. So, add the following CSS to your theme's style.css
.woocommerce-address-fields input[readonly="readonly"]{
opacity: 0.5;
}

$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
this solves the problem

This one worked for me.
https://www.sitekickr.com/snippets/woocommerce/make-checkout-field-read
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

Updating an ACF field after creating a Woocommerce attribute

I have a set of products (courses) that are dependent on a calendar.
The calendar is generated by a post type date which has an ACF field date_start and an associated_product
The logic is; the user creates a post-type Date which adds an attribute term to the associated product's attribute pa_dates.
I have hooked save_post and so far so good, the attribute is written to the product.
I then need to copy the date field to the term created $term_set this section doesn't produce anything. (I need this date to order the terms in my product)
Part of the problem is I have no way of interrogating the variables as this is all happening in a hook that has no way of printing output. (I have tried fwrite but with no result)
// Add terms on Date save.
function add_terms_to_date($post_ID) {
if ('date' !== get_post_type() ) {
return;
} else {
$product_id = get_field('associated_product',$post_ID);
$term_name = get_the_title($post_ID);
$taxonomy = 'pa_dates';
$term_set = wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
// up to here, works fine.
// now need to identify that term ($term_set)
// Then I need to write copy the date field to its ACF field in my Date attribute
$selector = "term_date_start"; // the ACF field in the term
$date_start = get_field('date_start', $post_ID); // the date field to be copied
$value = $date_start;
// This next line caused the issue leading to the question so is commented out
// update_field($selector, $value, $term_set);
// and this line is correct
update_field($selector, $value, $taxonomy."_".$term_set);
}
}
add_action('save_post', 'add_terms_to_date');
So, complicated question, simple answer.
The last line should read;
update_field($selector, $value, $taxonomy."_".$term_set);
to avoid confusion with post IDs (RTFM) ACF has a different system for identifying terms. ($taxonomy comes from the earlier line and is pa_dates)
I have edited the post above, just in case it can provide help to someone.

Wordpress admin: Get current open post (order) edit page meta value

We have a website running on woocommerce and we run a custom tax solution there. On frontend everything works perfect atleast in admin as we have set, that tax is calculated by store address, we get the basic store country into all of the orders. What we are trying to achieve is to replace this country in admin from current open order post meta. But we are unlucky and we are not getting any data. Is there any possiblity to get current open post meta value?
function custom_base_country($base_country ){
if (is_admin()){
global $woocommerce;
$order = $_GET['post'];
$country = get_post_meta($order->id, 'country_code', true);
$base_country = $country;
}
return $base_country;
}
add_filter('woocommerce_countries_base_country', 'custom_base_country', 10, 1);
Found out the answer by changing the code to $country = get_post_meta($order, 'country_code', true);

Woocommerce : How to add a custom meta_key to checkout billing_phone?

I want to enter the value of digits_phone meta key to be entered as billing_phone for every woocommerce order.
I came up with something like this but it did not work :
//Automatically add the digits phone number of the user, to woocommerce orders for every order
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
$data['billing_phone'] =['digits_phone'];
return $data;
}
can anyone please help me to figure this out?
I have never used the plugin myself, take this as a guideline
THIS CODE IS NOT TESTED !
Maybe this question is a better fit for wordpress.stackexchange.com
From the last comment of this post I see that there should be 2 user metadata related to some digits_phone: 'digits_phone' and 'digits_phone_no'
Assuming that the one we want is digits_phone, this code should be a hint in the right direction:
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
// save current user data in variable $current_user
$current_user = wp_get_current_user();
//get digits phone from db and save in variable $digits_phone
$digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );
// assign to POSTed array and return it
$data['billing_phone'] = $digits_phone;
return $data;
}
Also have a look at How can I convert woocommerce checkout fields in capital letters to get a better picture of manipulating POST data

Hide empty fields in email - Contact Form 7

I've spend almost the whole day looking for an answer, but nothing helped..
I made a large form with Contact Form 7, but parts of the form will hide, depends on your choice. For example, if you choose '2 persons', there will two parts show up.
But, if I fill in the fields for one person (so the other fields are empty and hidden), the fields will visible in the email. I only want to see the filled fields in the email.
I'm sorry if I'm a bit unclear. Could use some help, please.
Solved!
I found a solution by myself, which was already in Contact Form 7. The fields in the e-mail were not on the same line, so when I checked "Exclude lines with blank mail-tags from output", nothing happend. I've put it all on the same line and now it works.
Actually you need to implement your own custom email body component with,
add_filter('wpcf7_mail_components','my_custom_mail', 10,2);
function my_custom_mail($mail_component, $contact_form){
$mail_component['subject']; //email subject
$mail_component['sender']; //sender field (from)
$mail_component['body']; //email body
$mail_component['recipient']; //email recipient (to)
$mail_component['additional_headers']; //email headers, cc:, bcc:, reply-to:
$mail_component['attachments']; //file attachments if any
$key_values = array();
$tags = $contact_form->scan_form_tags(); //get your form tags
foreach($tags as $tag){
$field_name = $tag['name'];
if(isset($_POST[$field_name]) && !empty($_POST[$field_name])){
//get all the submitted fields form your form
$key_values[$field_name] = $_POST[$field_name];
}
}
//you have all the submitted field-name => value pairs in the array $key_values
//you can now reset you email body
$body = "Dear ".$key_values['your-name'].',';
...
$mail_component['body'] = $body;
return $mail_component;
}
To do this you can use the free Plug-in Conditional Fields for Contact Form 7 by Jules Colle.
With it you can create groups of information.
Then you create a checkbox and in the settings you choose which group will appear when it's selected.
Try adding this to your functions.php file:
add_filter( 'wpcf7_mail_components', 'remove_blank_lines' );
function remove_blank_lines( $mail ) {
if ( is_array( $mail ) && ! empty( $mail['body'] ) )
$mail['body'] = preg_replace( '|\n\s*\n|', "\n\n", $mail['body'] );
return $mail;
}
I have found the snippet here: https://wordpress.org/support/topic/plugin-contact-form-7-how-to-do-away-with-blank-lines-in-email-for-unfilled-form-items/ Depending on how you set up your email this might not work as this removes just the empty lines. Let me know if this works for you, and if not please provide the code from your email body, and an example email sent would be nice too.

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Resources