Im creating a plugin for connect Prestashop & Woocommerce. When I create a new user I update this customer with Woocommerce fields with update_user_meta like this:
update_user_meta( $user_id, “billing_email”, $customer[’email’] );
But I dont know what data type I need to billing & shipping state.
I know that is a string data, but with ISO Code like Contries or full State name?
update_user_meta( $user_id, “billing_state”, $customer[‘state’]);
What data type is $customer[‘state’]?
Best regards
]]>update_user_meta()
and update the meta data with a dropdown list, it updates the record with the index number (e.g. 12, meaning I have selected the 13th option from the dropdown) instead of the actual string value (e.g. ‘United States’). How can I fix this with the string value actually be saved?wp_usermeta table in MySQL record is originally like this,
+----------+---------+----------+-----------------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+----------+-----------------------+
| 519 | 11 | country | Canada |
+----------+---------+----------+-----------------------+
but when I go to the account.php page and update my field, using the dropdown and select ‘United States’, my meta_value is updated based on the index (int value 12, representing the 13th option), not the selected text.
+----------+---------+----------+-----------------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+----------+-----------------------+
| 519 | 11 | country | 12 |
+----------+---------+----------+-----------------------+
Here is the code I have on my functions.php
add_action('um_after_account_general', 'showUMExtraFields', 100);
function showUMExtraFields() {
$id = um_user('ID');
$output = '';
$names = array(
"country",
"office"
);
$fields = array();
foreach( $names as $name )
$fields[ $name ] = UM()->builtin()->get_specific_field( $name );
$fields = apply_filters('um_account_secure_fields', $fields, $id);
foreach( $fields as $key => $data )
$output .= UM()->fields()->edit_field( $key, $data );
echo $output;
}
add_action('um_account_pre_update_profile', 'getUMFormData', 100);
function getUMFormData(){
$id = um_user('ID');
$names = array(
"country",
"office"
);
foreach( $names as $name )
update_user_meta( $id, $name, $_POST[ $name ] );
}
This also relates to this topic:
https://gist.github.com/champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d#gistcomment-3520506
I am adding customer programmatically using wc_create_new_customer function, first name, last name, email is added successfully, but custom fields are not adding.
I have tried two ways
1st one:
`
$email= $arr_item->email;
$username = strstr($arr_item->email, ‘@’, true);
$password = wp_generate_password();
$args = array(
“first_name” => $arr_item->first_name,
“billing_first_name” => $arr_item->first_name,
“last_name” => $arr_item->last_name,
“billing_last_name” => $arr_item->last_name,
“billing_email” => $arr_item->email,
“billing_phone” => $arr_item->phone,
“agency_name” => $arr_item->agency_name,
“billing_company” => $arr_item->agency_name,
“agency_country” => $arr_item->agency_country,
“agency_city” => $arr_item->agency_city,
“agency_state” => $arr_item->agency_state
);
$customer_id = wc_create_new_customer( $email, $username, $password, $args );
2nd one
$customer_id = wc_create_new_customer( $email, $username, $password );
update_user_meta( $customer_id, ‘agency_name’,$arr_item->agency_name);
update_user_meta( $customer_id, ‘billing_company’, $arr_item->agency_name);
update_user_meta( $customer_id, ‘agency_country’, $arr_item->agency_country);
update_user_meta( $customer_id, ‘agency_city’, $arr_item->agency_city);
update_user_meta( $customer_id, ‘agency_state’,$arr_item->agency_state);
I am trying to add custom field after creating customer using wc_create_new_customer
Please help if anyone has any ideas.
Thanks in advance
]]>update_user_meta( $user_id, $key, $value );
inside a loop once for every user of the website?
(users are about 400)
]]>So, my question is: can somebody tell how to update my code? Or, would it be simpler to create a table with this data? Create a new post type?
(I thought a user meta field would be better, so that I wouldn’t clutter the database with another table, but maybe my reasoning is off.)
Anyway, here’s what I’m trying to accomplish:
1 – In the front end, the user chooses from a drop-down menu their selection for the day. For example: “Today, I ate an APPLE or ORANGE or BANANA”.
2 – The plugin saves the vote of fruit for the day in the user meta field I call myplugin-hist
.
3 – The next day, if the user chooses a new fruit, the plugin adds this new fruit to the myplugin-hist
so that we can see what the choice was yesterday and again today.
4 – I’ve tried various ways of storing the data, and I get arrays within arrays, not resembling the structure I have in mind, which should ideally have this structure:
array(
day 1 => apple
day 2 => banana
day 3 => apple
)
Here’s a sample of my code. So far, I’ve written just the code to save to the database (which is where I need help.)
// This function receives the (int)$fruit vote from another function
// (apple is 1, banana is 2, etc)
public function i_vote_for($fruit) {
global $MainPluginClass;
// First, we obtain the user's vote history
$hist = get_user_meta($this->ID, 'myplugin-hist', false);
// I added this, in case it's a brand-new user with no history
if (!array($hist)) $hist = array();
// This is a custom function that returns an int, to count day 1, day 2, etc
$d = $MainPluginClass->current_date();
//This is the part where I'm scrathcing my head.
//I've tried $hist[$d][] = $fruit;
//I've tried $hist[] = array ($d, fruit);
$hist[0][] = $fruit;
update_user_meta($this->ID, 'myplugin-hist', $hist);
// the $this->ID is a shorthand in my plugin for get_current_user...
}
]]>My code is
update_user_meta( $user_id, 'pll_filter_content', 'language_en' );
is this is a good way?
On databse I see that metakey name is “manageedit-postcolumnshidden”
So I think the code must be something like this ???
update_user_meta( $user_id, 'manageedit-postcolumnshidden', '???' );
I am using this plugin for user registration, and it is working fine. Now i wanted to add few custom meta keys. I have tried it using update_user_meta function.
Following is the flow i am using update_user_meta is:
1. Generating nonce
2. Registering user with basic parameters i.e username, first_name, email, display_name, user_url, user_pass and nonce.
But in registration i need to send few more parameters like phone_number, user_address, user_website. For that i am using update_user_meta.
3. Getting cookie from step 2, adding cookie to update_user_meta with meta_key and meta_value.
In the response of step 3, i am getting response ‘provide username’. When i provide it, getting response ‘provide email’. When i provide both then getting ‘provide nonce’. When i provide all these then i am getting response ‘User already exist’.
Can you suggest the solution on it. Kindly clarify me if my flow is incorrect.
Thanks,
SamKat
It doesn’t update the role for the registered user. It just adds None to the role after the registration is complete, but in essence I need the role which was selected to be saved when saving the username and other fields.
Here is what I have so far –
I have a registration form with fields which has radio choices like –
<label>
<input type="radio" name="user_role" id="Role1<?php $template->the_instance(); ?>_r" class="input"
<?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role1' ) echo 'checked="checked"'; ?> value="Role1"/>Role1
</label>
<label>
<input type="radio" name="user_role" id="Role2<?php $template->the_instance(); ?>" class="input"
<?php if (isset($_POST['user_role']) && $_POST['user_role'] == 'Role2' ) echo 'checked="checked"'; ?> value="Role2"/>Role2
</label>
This is what I have in another page that will process the incoming data.
The error module works fine, ie. It only shows the error message when I am not checking any role in my registration page.
<?php
function tml_registration_errors( $errors ) {
if ( !isset( $_POST['user_role'] ) )
$errors->add('empty_user_role', '<strong>ERROR</strong>: Please select a role.');
return $errors;
}
add_filter( 'registration_errors', 'tml_registration_errors' );
Saving module below doesn’t take effect and I am having issues. For debugging purpose I have the alert setup but that doesn’t alert show the alert box either, it is just bypassed and “None” is added to the role column in the dashboard view of user list.
function tml_user_register( $user_id ) {
$user = new WP_User( $user_id );
if ($_POST['user_role'] = 'Role1'){
echo "<script>alert('" . $_POST['user_role'] . "');</script>";
$user->remove_role( 'Default Role' );
$user->set_role( 'Role1' );
update_user_meta( $user_id, 'role', $_POST['user_role'] );
unset( $user );
}else{
echo "<script>alert('" . $_POST['user_role'] . "');</script>";
$user->remove_role( 'Default Role' );
$user->add_role( 'Role1' );
$user->set_role( $_POST['user_role'] ) ;
update_user_meta( $user_id, 'role', $_POST['user_role'] );
unset( $user );
}
}
add_action( 'user_register', 'tml_user_register' );
?>
Please help me with this, I am trying for past one or two days but in vain. I am not a hardcore PHP programmer but very much a beginner, any help is really appreciated. Thank you in advance.
]]>Everything is working fine if the shortcode is on a Post type, but if I put it on a ‘Page’ type, the form appears fine, but after clicking Update, I get the ‘Are you sure you want to do this?’ WordPress message.
What is the difference between the Post and the Page?
My code below:
function user_extraprofile_func( $atts ){
$user_id = get_current_user_id();
if ( isset($_POST['postcode']) ) {
update_user_meta( $user_id, 'postcode', $_POST['postcode'] );
}
$postcode = get_user_meta($user_id, 'postcode', true);
return '
<form method="POST" action="" id="extra-profile" >
<div>Postcode: <input name="postcode" id="postcode" type="text" value="'.$postcode.'" maxlength="10" /><br /></div>
<p><b>Important:</b> Due Date must be in "dd-mm-yyyy" format.</p>
<input type="submit" value="Update" />
</form>';
}
add_shortcode( 'user-extra-profile', 'user_extraprofile_func' );
The main thing I’m asking it why it doesn’t work in a Page type, but does everywhere else.
Thanks
]]>