Hidden checkout field Woocommerce for current user - php

I would like to collect and save Wordpress profile photo of the current user who submits checkout form in Woocommerce. I would achieve this with hidden checkout field. Here is what I have so far. Not sure how to get profile photo of the current user and output hidden photo :
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_hidden_field', 10, 1 );
function my_custom_checkout_hidden_field( $checkout ) {
// Get an instance of the current user object
$user = wp_get_current_user();
// Profile photo
// Output hidden photo
}

All you need to do is pass the current users email address into the get_avatar() function.
<?php
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
echo get_avatar( $current_user->user_email, 32 );
}
?>
Here are some links for specifics:
get_avatar();
wp_get_current_user();
To output the profile picture
<?php
echo '<img src="'. get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ) .'" />';
?>

Related

display Woocommerce subscription actions to any of custom page or dashboard

I am using WooCommerce subscriptions and am trying to create a custom flow in the dhasboard for the user.
Currently user logs in > dashboard shows, I have custom code to show if subscription status is "active" or "on-hold". If it is on hold, the user currently has to click on view subscription, then click on a listed subscription and then click renew in the actions section.
I want to move this actions button to renew an "on-hold" subscription out of the subscription-details.php file and into the dashboard.php file to reduce these steps.
Here is the snippet i've found that I think relates to the renew action button:
<?php do_action( 'woocommerce_subscription_before_actions', $subscription ); ?>
<?php $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() ); ?>
<?php if ( ! empty( $actions ) ) : ?>
<tr>
<td><?php esc_html_e( 'Actions', 'woocommerce-subscriptions' ); ?></td>
<td>
<?php foreach ( $actions as $key => $action ) : ?>
<?php echo esc_html( $action['name'] ); ?>
<?php endforeach; ?>
</td>
</tr>
<?php endif; ?>
<?php do_action( 'woocommerce_subscription_after_actions', $subscription ); ?>
I tried to bring this into the dashboard.php file, however I get an error saying the site is experiencing technical difficulties.
Any ideas on how I could bring this renewal action button into the dashboard.php file instead?
Thanks in advance for any help!!!
If you want to show any of action in your custom page or dashboard then use below code and set action according to your requirements
function addCancelButton($subscription) {
$actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() );
if(!empty($actions)){
foreach ( $actions as $key => $action ){
if(strtolower($action['name']) == "cancel"){
$cancelLink = esc_url( $action['url'] );
echo "<a href='$cancelLink' class='button cancel'>".$action['name']."</a>";
}
}
}
}
add_action( 'woocommerce_my_subscriptions_actions', 'addCancelButton', 10 );
If you want to edit My account page then i suggest to use this hook in your child theme
woocommerce_account_dashboard
Here is code for same
add_action( 'woocommerce_account_dashboard','add_account_content_kiki' );
function add_account_content_kiki() {
if( has_active_subscription() ){ // Current user has an active subscription
echo '<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info"><a class="woocommerce-Button button" href="www.google.com">Test Now</a>Test link - shop now</div>';
// Example of displaying something
echo 'You have active subscription';
}
}
you have to add this function also in function file
function has_active_subscription( $user_id='' ) {
// When a $user_id is not specified, get the current user Id
if( '' == $user_id && is_user_logged_in() )
$user_id = get_current_user_id();
// User not logged in we return false
if( $user_id == 0 )
return false;
return wcs_user_has_subscription( $user_id, '', 'active' );
}
Reference of hook - https://docs.woocommerce.com/wc-apidocs/hook-docs.html
All hook you can use from here for woo commerce my account page - https://businessbloomer.com/woocommerce-visual-hook-guide-account-pages/

I want to update WooCommerce status for a order with the user name

I am able to update status using this code
In this image highlighted text is username of currently logged in user, when I changed status from dashboard it shows me name, but when I change status using code it won't show any name.
I want username should be display like in this screenshot:
add_filter('woocommerce_new_order_note_data', 'modify_added_by');
function modify_added_by($args) {
$user = get_user_by('id', get_current_user_id());
$comment_author = $user->display_name;
$comment_author_email = $user->user_email;
$args['comment_author'] = $comment_author;
$args['comment_author_email'] = $comment_author_email;
}
Try this code
You can use the following hooked function to get the shop manager user name in the order note:
add_filter( 'woocommerce_new_order_note_data', 'filter_woocommerce_new_order_note_data', 10, 2 );
function filter_woocommerce_new_order_note_data( $args, $args2 ) {
if( ! $args2['is_customer_note'] && is_user_logged_in() && current_user_can( 'edit_shop_order', $args2['order_id'] ) ){
$user = get_user_by( 'id', get_current_user_id() );
$args['comment_author'] = $user->display_name;
$args['comment_author_email'] = $user->user_email;
}
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related thread: Add the Shop Manager username to Woocommerce Admin Order notes

Adding a hidden checkout field in WooCommerce?

I want to include a link to a profile of the current user who submitted a checkout form through WooCommerce.
That is, to place automatically a current user’s author link like this in the hidden field: example.com/author/username
I want to achieve this by adding a hidden field in checkout form. So to get a link I would write something likes this:
<?php
$currentUser = get_current_user_id();
$user = get_user_by( 'id', $currentUser );
$userUrl = get_bloginfo( 'home' ) . '/author/' . $user->user_login;
echo $userUrl;
?>
My question is how can I create this type of hidden field in checkout form?
With a custom function hooked in woocommerce_after_order_notes action hook, you can also directly output a hidden field with this user "author link" as a hidden value, that will be submitted at the same time with all checkout fields when customer will place the order.
Here is that code:
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_hidden_field', 10, 1 );
function my_custom_checkout_hidden_field( $checkout ) {
// Get an instance of the current user object
$user = wp_get_current_user();
// The user link
$user_link = home_url( '/author/' . $user->user_login );
// Output the hidden link
echo '<div id="user_link_hidden_checkout_field">
<input type="hidden" class="input-hidden" name="user_link" id="user_link" value="' . $user_link . '">
</div>';
}
Then you will need to save this hidden field in the order, this way:
add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_hidden_field', 10, 1 );
function save_custom_checkout_hidden_field( $order_id ) {
if ( ! empty( $_POST['user_link'] ) )
update_post_meta( $order_id, '_user_link', sanitize_text_field( $_POST['user_link'] ) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and working
Add this to your functions.php file (or a plugin file,etc.)
add_action( 'woocommerce_after_order_notes', 'hidden_author_field' );
function hidden_author_field( $checkout ) {
$currentUser = get_current_user_id();
$user = get_user_by( 'id', $currentUser );
$userUrl = get_bloginfo('home').'/author/'.$user->user_login;
woocommerce_form_field( 'hidden_author', array(
'type' => 'hidden',
'class' => array('hidden form-row-wide'),
), $userUrl);
}
This code is untested, more reading here https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ and here http://woocommerce.wp-a2z.org/oik_api/woocommerce_form_field/. Please let me know if this worked for you and if not what the problem is.

WordPress i want custom link in user list when i click the link get the images by user

I try to add link in user list by adding this code
function frontend_profile_action_link($actions, $user_object) {
$actions['view profile'] = "<a class='view_frontend_profile' href='".admin_url().'user-detail.php'."'>".__('View Profile', 'frontend_profile')."</a>";
return $actions;
}
add_filter('user_row_actions', 'frontend_profile_action_link', 10, 2);
I want to add custom link in user list like default edit link and then when I click this link get all images by user
I think you should tackle
<?php $user = get_user_by( $field, $value ); ?>
or if you specified a custom field in the registration form you can use
<?php get_userdata( $field ); ?>

WordPress - How can I update user meta data outside of the user's profile? I've provided the code I'm using to display

Using user meta data, I am running a function that is hidden in a user’s profile that allows them to have a checklist. I’m using Theme My Login and not including the checklist on the user’s profile page when they edit/view their profile.
I am trying to display the checklist on it’s own page. I can get it to display but can’t seem to get the Submit button to work. Can someone point me in the right direction?
Thanks!
Code example in functions.php:
function my_show_extra_profile_fields( $user ) { ?>
<div><input type="checkbox" name="checkbox1" id=" checkbox1 " value="yes" <?php if (esc_attr( get_the_author_meta( "checkbox1", $user->ID )) == "yes") echo "checked"; ?> />
Checkbox 1
</div>
<button type="submit">Save</button>
<?php }
?>
<?php
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, checkbox1, $_POST['checkbox1'] );
}
?>
Code example to display the extra profile field on my page template:
<?php my_show_extra_profile_fields(); ?>
EDIT: Forgot my '', added them back in
You have $_POST[checkbox1] it should be $_POST['checkbox1']
Assuming you created the field already, you can use wp_update_user from the codex.
$user_id = wp_update_user( array( 'ID' => $user_id, 'checkbox1' => $_POST['checkbox1'] ) );
if ( is_wp_error( $user_id ) ) {
// There was an error, probably that user doesn't exist.
} else {
// Success!
}

Categories