I am working on a woocommerce project and my requirement is that whenever a new user registration occurs there should an account number generated which should be mailed to him so I am trying to use the update_user_meta function but not understanding how to get the User_id for somebody who has not yet registered
Please check my code and advise
add_action('woocommerce_register_form', 'vmart_add_account_number');
function vmart_add_account_number() {
global $wpdb;
$userdata = array();
$user_id = wp_insert_user($userdata);
$account_number = 1;
update_user_meta($user_id, 'account_number', $account_number);
$account_number++;
}
I have also tried this code
add_action('user_register', 'vmart_add_account_number', 10, 1);
function vmart_add_account_number($user_id)
{
$accountnumber = 1;
update_user_meta($user_id, 'account_number', $accountnumber);
}
For this thing you can use standard wordpress hook:
// Register subcustomer to same customer
add_action('user_register', 'vmart_add_account_number', 10, 1);
function vmart_add_account_number($user_id)
{
update_user_meta($user_id, 'account_number', $account_number);
}
Related
i'm having a odd issue with WooCommerce.
I have a plugin I developed that sends data from WooCommerce orders to HubSpot. The plugin needs to fire whenever a new order is placed OR an existing order is updated . I've been using the following truncated code to try to achieve this:
add_action('save_post', 'printout', 10, 3);
function printout($post_ID, $post, $update)
{
if ("shop_order" == $post->post_type) {
$msg = $post_ID;
delegator($msg);
}
}
function delegator($order_id){
$order = get_woocommerce_order($order_id);
// assigns the actual data of the order to the $data variable
$data = get_woocommerce_order_data($order);
// assign User e-mail for search
$email = $order->get_billing_email();
//$email = get_post_meta( $order_id, '_billing_email', true );
}
The problem i'm having is that upon new Orders being made, WordPress can't get the order details, specifically a e-mail (it returns null). The hook does return an order number, so I don't think it's the hook doing this but something else.
The odd part is if after the order is placed and I go into the Order page and just hit update on the order, it works!
So I'm wondering what I'm missing / what's wrong here. Does the flow of the hook fire before WooCommerce can make database calls?
Thanks for any help. Been driving me mad!
For orders added/updated through admin, use the code below
add_action('save_post_shop_order', 'backend_delegator', 10, 3);
function backend_delegator($post_id, $post, $update) {
// Orders in backend only
if (!is_admin())
return;
// Get an instance of the WC_Order
$order = new WC_Order($post_id);
//Fired when create a new order
if (!$update) {
$email = $order->get_billing_email();
}
if ($update) {
$email = $order->get_billing_email();
}
}
For orders placed from checkout, use the code below.
add_action('woocommerce_new_order', 'frondend_delegator', 10, 2);
function frondend_delegator($order_id, $order) {
$email = $order->get_billing_email();
}
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 found many questions like this but nothing matching to my requirement.Here my requirement is to lock a specific page named settings. It's not to be deleted by others.But it should be able to edit. Is there any way to lock a specific page using its page id or name.
Create a hook in themes function file as per below:
function restrict_page_deletion($post_ID){
$user = get_current_user_id();
$restricted_pageId = 4;
if($post_ID == $restricted_pageId)
{
echo "You are not authorized to delete this page.";
exit;
}
}
add_action('before_delete_post', 'restrict_page_deletion', 10, 1);
Pass your page id to a restricted_pageId variable.
If you want to implement this functionality for multiple pages then use the array in place of the variable.
Admin can move a page to trash but admin will not able to delete it.
If you want to block admin for trach functionality then call a hook on "wp_trash_post" action.
add_action('wp_trash_post', 'restrict_page_deletion', 10, 1);
function wpse_312694_restrict_page_deletion( $caps, $cap, $user_id, $args ) {
$post_id = $args[0];
if ( $cap === 'delete_post' && $post_id === 117 ) {
$caps[] = 'do_not_allow';
}
return $caps;
}
add_filter( 'map_meta_cap', 'wpse_312694_restrict_page_deletion', 10, 4 );
The Wordpress Ultimate Member (UM) plugin allows us to set newly registered users to ‘Pending’ through wp-admin.
The Pending function does 2 things:
Sets the user status to ‘awaiting admin approval’
Sends the user an email
See here:
function pending(){
global $ultimatemember;
$this->set_status('awaiting_admin_review');
$ultimatemember->mail->send( um_user('user_email'), 'pending_email' );
But, there is no way to set existing users to ‘Pending’ admin approval if users edit an existing account.
I’m not strong at PHP, but I figured out a hook into the ‘um_user_edit_profile’ action, so that if existing users edit their profile the status is changed to ‘pending’.
See here:
// Set profile to under pending after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_super_admin() ) {
return false;
} else {
global $ultimatemember;
$ultimatemember->user->pending();
}
}
Unfortunately, I have just discovered that the ‘um_user_edit_profile’ action is also used at registration, so my hook triggers at registration too which results in two emails being sent.
I’ve tried to overcome this by:
Adding and if statement so the action is only triggered for logged in users:
// Set profile to under review after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_super_admin() ) {
return false;
} else {
if ( is_user_logged_in() ) {
global $ultimatemember;
$ultimatemember->user->pending();
}
}
By trying to set the status to ‘awaiting admin approval’ without including the email:
// Set profile to under review after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_super_admin() ) {
return false;
} else {
global $ultimatemember;
$ultimatemember->set_status('awaiting_admin_review');
}
}
// Set profile to under review after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_super_admin() ) {
return false;
} else {
global $ultimatemember;
$this->set_status('awaiting_admin_review');
}
}
I’ve also tried too many other variations to include and all of them break the site.
So, I’m asking the community for some support/ pointers on how to use the pending function without the email being sent or how to set the status to 'awaiting admin review' using my hook.
I’ve been using the UM github repository to help me research the UM code:
After quite a bit of further research I managed to identify the um_submit_form_profile action which is triggered when users edit their profile, but not used at registration, so the duplicate email issue at registration is solved.
Here is my original hook into the new action. This sets the user status to pending admin review when users edit their profile.
// Set profile to under pending after edits
add_action('um_submit_form_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_super_admin() ) {
return false;
} else {
global $ultimatemember;
$ultimatemember->user->pending();
}
}
Thanks to Champ Campo, one of the plugin authors, I got an even better fix that just changes the account status to awaiting admin review and does nothing else.
// Set profile to under review after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook');
function um_post_edit_pending_hook( $args ){
$user_id = $args['user_id'];
if ( is_super_admin() ) {
return;
}
update_user_meta( $user_id, 'account_status', 'awaiting_admin_review');
}
I couldn't make this work in UM 2+. However, for anyone still struggling with this, I did find a way to do it, by tacking on the user meta change to the end of an email notification snippet which I found. I also adjusted the email to include the 'description' field from the profile, so admin can see immediately if it has been changed to include anything undesirable.
/*function to notify admin of profile changes*/
add_action( 'um_after_user_updated', 'my_after_user_updated', 10, 3 );
function my_after_user_updated( $user_id, $args, $userinfo ) {
um_fetch_user($user_id);
$groupMember = um_user('display_name');
$groupMmbr_name = um_user('user_login');
$groupMmbr_hidden = um_user('hide_in_members');
$groupMmbr_status = um_user('account_status');
$groupMmbr_desc = um_user('description');
$loggedIn_user = wp_get_current_user();
$loggedIn_userEml = $loggedIn_user->user_email;
$headers = array('Content-Type: text/html; charset=UTF-8', 'Cc:your-cc-address#yourwebsite.com', 'Bcc:',
'From:Your Website <website#yourwebsite.com>', 'Reply-To:No-reply Email<noreply#yourwebsitesite.com>');
wp_mail( 'your-admin-email-address#yourwebsite.com', 'The member profile '.$groupMember.' has been updated.',
'The <b>'.$groupMember.'</b> profile was updated by the user with this email address: '.$loggedIn_userEml.'. <a href="https://yourwebsite.com/member/'.$groupMmbr_name.'/?profiletab=main&um_action=edit"><br>
<b>Click here</b></a> to visit this profile online.<br><br><i style="color:#767676; font-size:11px; text-align:center;"><b>NOTE:</b> you must be logged in first to see this profile.</i>
<br><br>The new profile description is:<br><br>'.$groupMmbr_desc.'<br><br>',$headers);
update_user_meta( $user_id, 'account_status', 'awaiting_admin_review');
}
<?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..