I want to pre-populate the values for the checkout's billing fields to the DB stored values of the user before his first purchase.
I've tried the following code:
add_filter( 'woocommerce_checkout_fields' , function ( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name';
$fields['billing']['billing_first_name']['default'] = wp_get_current_user()->user_firstname;
return $fields;
});
I've read about this solution in an other post. Placeholder works great, but the value doesn't.
Also, the WooCommerce Doc (Lesson 1) doesn't say about anything the array value 'default'
You're pretty close. This worked for me. I don't know if it was necessary, but I used a named function and only get the user_firstname property if the user exists.
add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
$user = wp_get_current_user();
$first_name = $user ? $user->user_firstname : '';
$fields['billing']['billing_first_name']['placeholder'] = 'First Name';
$fields['billing']['billing_first_name']['default'] = $first_name;
return $fields;
}
Related
I'm trying to update the core user fields in WooCommerce when I edit a users profile, however, when I save, only the user meta saves.
But the wp_update_user is not saving the distributor name in the billing_company field.
The code is fired in a function called from
add_action('edit_user_profile_update', 'user_profile_update_action');
The code in the function is ...
add_action('edit_user_profile_update', 'user_profile_update_action');
function user_profile_update_action($user_id) {
if(isset($_POST['distributor_id']) AND $_POST['distributor_id'] == "|"){
delete_metadata( $user_id, 'distributor_id', '');
delete_metadata( $user_id, 'distributor_name', '');
}else{
$distributordata = explode("|", $_POST['distributor_id']); // Split the array
update_user_meta($user_id, 'distributor_id', $distributordata[0] );
update_user_meta($user_id, 'distributor_name', $distributordata[1] );
wp_update_user(array('ID' => $user_id, 'billing_company' => $distributordata[1]));
}
}
I've tried update user meta and wp update user but neither want to save billing company.
$customer = new WC_Customer( $user_id );
$customer->set_billing_company($distributordata[1]);
$customer->save();
I have problem with woocommerce order in admin I want the billing_address_2 show at the end of the page as exmple bellow.
can any one please help me.
The core file that is responsible to displayinng that fields is located in WooCommerce plugin under: includes/admin/meta-boxes/class-wc-meta-box-order-data.php.
The only available and efficient hook is: woocommerce_admin_shipping_fields.
But you will only be able to change the admin billing fields order using something like:
add_filter( 'woocommerce_admin_billing_fields' , 'change_order_admin_billing_fields' );
function change_order_admin_billing_fields( $fields ) {
global $the_order;
$address_2 = $fields['address_2'];
unset($fields['address_2']);
$fields['address_2'] = $address_2;
return $fields;
}
Which will give you something like:
So as you can see you will not get the billing address_2 field to be displayed after the transaction ID as you wish, but only under the billing phone field.
Addition - Showing the billing_address_2 field before billing_country field:
add_filter( 'woocommerce_admin_billing_fields' , 'change_order_admin_billing_fields' );
function change_order_admin_billing_fields( $fields ) {
global $the_order;
$sorted_fields = [];
$address_2 = $fields['address_2'];
unset($fields['address_2']);
foreach ( $fields as $key => $values ) {
if( $key === 'country' ) {
$sorted_fields['address_2'] = $address_2;
}
$sorted_fields[$key] = $values;
}
return $sorted_fields;
}
I'm looking for a way to hide the billing address on the checkout page of my woocommerce theme if the user has already filled up the billing form (from a previous order or if the user has done it previously from the "my account" page).
I've found ways to hide the billing / shipping form completely on the checkout page if the user is logged in (see below), however I can't find a way to do the above.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() ){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
Any idea?
Thank you!
It will depend what you consider to be a fully completed address i made snippet function you can use to go further with.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() && !has_billing()){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
// Check the meta of Postcode and Country if they are entered.
function has_billing($user_id = false){
if(!$user_id)
$user_id = get_current_user_id();
$shipping_postcode = get_user_meta( $user_id, 'billing_postcode', true );
$shipping_country = get_user_meta( $user_id, 'billing_country', true );
// Fetch more meta for the condition has needed.
if($shipping_postcode && $shipping_country){
return true;
}
return false;
}
Note: the prefix shipping_ there is one for billing ( billing_ ).
Edit: Here the meta key billing_address_1 and billing_address_2 always the prefix can be either billing_ or shipping_
Also if for some reason you don't have a shipping or billing address associated in the user meta keys but the customer once did an order you can check this code to fetch order address.
Woocommerce WC_Order get_shipping_address() not returning as array (old post might not be valid anymore)
I couldn't seem to make this code to work. I'm trying to change the notification email on Gravity Forms/Wordpress by dynamically retrieving an author's email address.
The code below works if I hard-code an author's email address to the '$notification['to']'.
I'm just not sure what I must be doing wrong.
Appreciate the help. Thanks.
add_filter( 'gform_notification_19', 'change_notification_email', 10, 3 );
function change_notification_email( $user_email, $notification, $form, $entry ) {
//There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'Applicant' ) {
global $post;
$author_id=$post->post_author;
$user_email = get_the_author_meta( 'user_email' , $author_id );
// toType can be routing or email
$notification['toType'] = 'email';
$notification['to'] = '$user_email';
}
return $notification;
}
According to the codex this should work. Try also to print the variables to see if they are getting the proper values. Also, is your filter correct?
add_filter( 'gform_notification_19', 'change_notification_email', 10, 3 );
function change_notification_email( $user_email, $notification, $form, $entry ) {
//There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'Applicant' ) {
global $post;
$author_id=$post->post_author;
$user_email = get_the_author_meta( 'user_email' , $author_id );
// toType can be routing or email
$notification['toType'] = 'email';
$notification['to'] = $user_email;
}
return $notification;
}
WooCommerce under Settings/Account has an option named
Automatically generate username from customer email
but the username it generates is not the full email address.
my.email#example.com
becomes myemail as a username.
What method would I need to hook into to override the user generation so that the
full email is set as the username?
You can use something like this in your functions.php. The woocommerce_new_customer_username filter could be used to change the generated username. If you just return the customer email as the username, it will be used instead of the generated one (in your case the name before #).
function my_new_customer_username( $username, $email, $new_user_args, $suffix ) {
return $email;
}
add_filter( 'woocommerce_new_customer_username', 'my_new_customer_username', 10, 4 );
Simply use the hook pre_user_login
add_filter( 'pre_user_login' , 'wpso_same_user_email' );
function wpso_same_user_email( $user_login ) {
if( isset($_POST['billing_email'] ) ) {
$user_login = $_POST['billing_email'];
}
if( isset($_POST['email'] ) ) {
$user_login = $_POST['email'];
}
return $user_login;
}
Sorry for reviving an old thread, but it seems this is the best option (goes in your child functions.php or custom plugin)
add_filter( 'woocommerce_new_customer_data', function( $data ) {
$data['user_login'] = $data['user_email'];
return $data;
} );
here are 2 source references: 1 and 2