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();
Related
I'm trying to minimize the billing address from checkout 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 have tried this method to hide the billing address completely but it doesn't work at all
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;
}
What I'm doing wrong?
You are unsetting the form fields that are being submitted. If you hide them in CSS that should work or you will need to populate the forms dynamically when you click the checkout button.
In WooCommerce I am using Boss Learndash plugin and in a template file of this plugin plugins/boss-learndash/templates/learndash/single-sfwd-course.php, I am trying to add once more button to users who have bought the course/product. For that Im trying to call wc_customer_bought_product woocommerce function in the template but seems like it is not able to call that function.
I tried by adding global $woocommerce; and also tried by wc->user->wc_customer_bought_product but couldnt fix it.
What I am doing wrong?
The wc_customer_bought_product() function is not a method of any WooCommerce class. It's just a conditional function with 3 arguments $customer_email, $user_id and $product_id:
wc_customer_bought_product( $customer_email, $user_id, $product_id );
It will return a boolean true or false, so you will use it in an if statement as a conditional function.
To get the user ID and the customer email, you can use:
// Get the current user data:
$user = wp_get_current_user();
$user_id = $user->ID; // Get the user ID
$customer_email = $user->user_email; // Get the user email
// OR
// $customer_email = get_user_meta( $user->ID, 'billing_email', true ); // Get the user billing email
// The conditional function (example)
// IMPORTANT: $product_id argument need to be defined
if( wc_customer_bought_product( $customer_email, $user_id, $product_id ) ) {
echo "Has bought the product";
} else {
echo "Has not bought the product yet";
}
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 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;
}
<?php
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$user_fields = get_user_meta( $user_id, , 'true' );
echo $user_fields;
?>
It showing 'Array'. I would want to know the field to be used on counting the number of times the user logins.
use:
var_dump($user_fields)
It would show you the variable content !
use
$user_fields = get_user_meta( $user_id );
print_r($user_fields);
it will showing all meta field regarding $user_id
In wordpress there is no any pre-defined functionality is available
for get user login count.. You have to options for doing this..
either create a plugin or write the follwing custom code on
functions.php file..
Here I have create a variable user_login_count in user meta key in regards to user id, so everytime user successfully login, i m increasing the user count by 1..
function ulc_my_login_redirect($redirect_to, $request, $user) {
//is there a user to check?
global $user, $wpdb;
if (isset($user->ID)) {
if ( get_user_meta($user->ID, 'user_login_count', true) ){
$user_login_count = get_user_meta($user->ID, 'user_login_count', true);
$user_login_count++;
update_user_meta($user->ID, 'user_login_count', $user_login_count);
}else{
add_user_meta($user->ID, 'user_login_count', '1');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'ulc_my_login_redirect', 10, 3);
By using this filter you can get the key value of 'user_login_count' user get_post_meta function..