Wordpress - get user list of following users - php

I'm using the plugin "user following system" and all I need to get is a list of users which I follow. I tried it with the snippet below but it always returns an array!
What am I doing wrong?
function pwuf_get_following( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = get_user_meta( $user_id, '_pwuf_following', true );
if ( empty( $following ) ) {
return;
}
return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
ob_start(); ?>
<ul>
<?php
$following = get_user_meta( $user_id, '_pwuf_following', true );
foreach( $following as $user ) { ?>
<li><?php echo $user->first_name; ?> <?php echo $user->last_name; ?></li>
<?php } ?>
</ul>
<?php
return ob_get_clean();
}
add_shortcode( 'following', 'pwuf_get_following' );

Related

Display problem depending on the customer's role - woocommerce

I have a problem on the "my account" page of my woocommerce store.
Indeed, when I log in with a new customer account that has never placed an order, I have 3 content containers that disappear.
After testing, they only appear if I place an order with the account in question.
Do you have any idea where this could be coming from?
The 3 divs in question correspond to :
the navigation menu of the page my account that disappears only on the dashboard tab (it reappears on all other tabs - my orders, my addresses ...)
a div with the recently viewed products (see code n°1 below)
a div with the articles recently written by the customer (see code n°2 below)
When an order is placed, all the content reappears.
I also have a div with the recent orders which strangely, does not have any problem of display (see code n°3 below)
Do not hesitate to ask me for clarifications, or if you think that the error comes from a tag elsewhere in my code (a tag of authorization according to the role for example?)
In advance, Thank you very much for your help.
add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
function bbloomer_recently_viewed_shortcode() {
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
if ( empty( $viewed_products ) ) return;
?>
<div class="col-23">
<?php $title = '<h3>Produits consultés récemment</h3>';?>
<?php $product_ids = implode( ",", $viewed_products );?>
<?php return $title . do_shortcode("[products ids='$product_ids' limit='4' columns='4' orderby='post__in' class='recentes-vues']");?>
</div>
<?php }
function custom_track_product_view() {
if ( ! is_singular( 'product' ) ) {
return;
}
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) )
$viewed_products = array();
else
$viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );
if ( ! in_array( $post->ID, $viewed_products ) ) {
$viewed_products[] = $post->ID;
}
if ( sizeof( $viewed_products ) > 15 ) {
array_shift( $viewed_products );
}
// Store for session only
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
add_action( 'template_redirect', 'custom_track_product_view', 20 );
// PRODUIT RECENT PAGE COMPTE
add_action( 'woocommerce_account_dashboard' , 'recentes_vues', 5 );
function recentes_vues() {
echo do_shortcode('[recently_viewed_products]');
}
add_action( 'woocommerce_account_dashboard' , 'recent_posts', 3 );
function recent_posts() {
if ( is_user_logged_in()) :
global $current_user;
wp_get_current_user();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
?><div id="recentposts">
<?php
if ($author_posts->found_posts) {
?>
<ul class="liststylenone">
<?php
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?></ul><?php
}
else {
echo '<p>Aucun article trouvé 😞</p>';
}
else :
echo "not logged in";
endif;
?>
add_action( 'woocommerce_account_dashboard', 'recent_order', 2 );
function recent_order(){
// For logged in users only
if ( is_user_logged_in() ) :
$user_id = get_current_user_id(); // The current user ID
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( $user_id );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
if ( is_a( $last_order, 'WC_Order' ) ) {
$order_id = $last_order->get_id(); // Get the order id
$order_data = $last_order->get_data(); // Get the order unprotected data in an array
$order_status = $last_order->get_status(); // Get the order status
$date_created = $last_order->get_date_created();
$order_total = $last_order->get_total();
}
?>
<div class="row last-order">
<div class="col-md-7">
<div class="col-3">
<div class="col-5">
<div class="col-md-4 order-status-box">
<h6 class="status">État : <?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></h6>
</div>
<p class="totalcmdd"><?php echo $order_total."€"; ?></p>
<?php
setlocale(LC_TIME, 'fr_FR');
date_default_timezone_set('Europe/Paris');
echo utf8_encode(strftime('%d %B %Y', strtotime($date_created)));
//echo date('d-F-Y', strtotime($date_created)); ?>
</p>
</div>
<div style="display: flex;flex-direction: row;justify-content: space-evenly;align-items: center;margin-right: 1.5em;margin-left: 1.5em;padding: 0.3em;background-color: white;text-transform: uppercase;text-decoration: none;font-size: 13px;">
<?php foreach ($last_order->get_items() as $item) :
$product = $item->get_product();
$thumbnail = $product->get_image(array(50, 50));
if ($product->get_image_id() > 0) {
$item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>'; // You had an extra variable here
}
echo $item_name . $item->get_name();
endforeach;
endif;
?>

Show Dokan vendor country on WooCommerce product catalog

Can someone help me how can i fix this code
instead of showing the country it shows the author
can you guys tell me what i did wrong on this code?
add_action( 'woocommerce_after_shop_loop_item_title','sold_by' );
function sold_by(){
?>
<?php
global $product;
$seller = get_post_field( 'post_author', $product->get_id());
$author = get_user_by( 'id', $seller );
$store_info = dokan_get_store_info( $author->ID );
$address = dokan_get_store_info( $author->address );
?>
<span class="details">
<?php printf( 'Origin: %s', $author->display_name, $address ); ?>
</span>
<?php
}
See this screenshot showing origin as author instead of country:
Try the following :
add_action( 'woocommerce_after_shop_loop_item_title','dokan_store_country' );
function dokan_store_country(){
global $product;
$seller_id = get_post_field( 'post_author', $product->get_id());
$seller = new WP_User( $seller_id );
$countries = WC()->countries->get_countries();
$store_data = dokan_get_store_info( seller_id );
if ( isset( $store_data['country'] ) ) {
$store_country = $store_data['country']; // Try to get seller store country
} elseif( isset( $countries[$seller->billing_country] ) ) {
$store_country = $countries[$seller->billing_country]; // Try to get seller billing country
} else {
return; // Exit
}
echo '<span class="details">' . sprintf( __('Origin: %s'), $store_country ) . '</span>';
}
Untested, it could work.

WordPress: Order admin user column by number

I want to order an admin column in the user overview list by a custom ID.
To do so, I added a function to sort the column but it doesn't work as expected.
It kind of sorts the fields but in a strange way. I guess there is an other sorting in place or the function has an error?!
This is the code for adding the ID to the user:
add_action( 'edit_user_profile', 'wcv_store_custom_vendor_id', 0 );
//add_action( 'wcv_admin_after_store_address', 'wcv_store_custom_vendor_id' );
function wcv_store_custom_vendor_id( $user ) {
?>
<table class="form-table">
<tbody>
<tr>
<th><label for="_wcv_custom_vendor_id"><?php _e( 'Vendor ID', 'wcvendors-pro' ); ?></label></th>
<td><input type="text" name="_wcv_custom_vendor_id" id="_wcv_custom_vendor_id" value="<?php echo get_user_meta( $user->ID, '_wcv_custom_vendor_id', true ); ?>" class="regular-text"></td>
</tr>
</tbody>
</table>
<?php
}
// Save the details on the back end when updating the user
add_action( 'wcvendors_update_admin_user', 'save_wcv_custom_vendor_id' );
function save_wcv_custom_vendor_id( $user_id ){
if ( isset( $_POST['_wcv_custom_vendor_id'] ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', $_POST['_wcv_custom_vendor_id'] );
}
Here's what I have so far:
// Add column to admin
add_action('manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __('Vendor-ID');
return $columns;
}
// fetching the status, thanks to LoicTheAztec
add_filter('manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( 'wcv_custom_vendor_id' == $column_name ) {
if( get_user_meta( $user_id, '_wcv_custom_vendor_id', true ) != '' ) {
$value = '<span style="color:green;font-weight:bold;">'.get_user_meta( $user_id, '_wcv_custom_vendor_id', true ).'</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = 'Vendor-ID';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );
// second try to sort by number, no effect?!
function sort_datanowa_column( $vars ) {
if ( isset( $vars['orderby'] ) && 'Vendor-ID' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_wcv_custom_vendor_id', // Find correct meta field key
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'request', 'sort_datanowa_column' );
I checked a lot of snippets and the last function should sort by number. But I think it has no effect at all.
Is there anythin I miss?
Use pre_get_users opposite request
For empty values ​​for the meta_key we use the following: update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
So you get:
// Add column to admin
function add_custom_vendor_id_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_action( 'manage_users_columns', 'add_custom_vendor_id_column', 10, 1 );
// fetching the status, thanks to LoicTheAztec
function add_data_to_vendor_id_column( $value, $column_name, $user_id ) {
if ( $column_name == 'wcv_custom_vendor_id' ) {
$vendor_id = (int) get_user_meta( $user_id, '_wcv_custom_vendor_id', true );
// If empty
if ( empty ($vendor_id ) ) {
update_user_meta( $user_id, '_wcv_custom_vendor_id', 0 );
}
if ( $vendor_id && $vendor_id != 0 ) {
$value .= '<span style="color:green;font-weight:bold;">' . $vendor_id . '</span>';
} else {
$value = '<span class="" style="color:red;font-weight:bold;">No ID!</span>';
}
}
return $value;
}
add_filter( 'manage_users_custom_column', 'add_data_to_vendor_id_column', 10, 3);
// make admin colum sortable
function fc_my_sortable_cake_column( $columns ) {
$columns['wcv_custom_vendor_id'] = __( 'Vendor-ID', 'woocommerce');
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column', 10, 1 );
// orderby
function action_pre_get_users( $query ) {
if ( !is_admin() )
return;
$orderby = $query->get('orderby');
if ( $orderby == 'Vendor-ID' ) {
$query->set( 'orderby' , 'meta_value_num' );
$query->set( 'meta_key', '_wcv_custom_vendor_id' );
}
}
add_action( 'pre_get_users', 'action_pre_get_users', 10, 1 );

Wordpress - How to display 10 users only?

Sorry I'm newbie with Wordpress, How to display 10 users only and the others inside "see more".
I searched more times before asking but with nothing!
Thanks in advance.
<?php
$post_id = get_the_ID();
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
endforeach; ?>
<?php } ?>
first off all try to add an index in your loop
<?php
$post_id = get_the_ID();
$i = 0;
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
if ($i++ == 9){
echo "<a href='#'>see more</a>";
break;
}
endforeach; ?>
<?php } ?>
and then if $i get 9 the loop will be break

Wordpress - Send email to users when they get new followers

I'm using a simple plugin "user following system" And all what i need to do is sending an email after the users get a new followers.
I think this is the important part of the plugin code:
function pwuf_follow_user( $user_id, $user_to_follow ) {
$following = pwuf_get_following( $user_id );
if ( $following && is_array( $following ) ) {
$following[] = $user_to_follow;
} else {
$following = array();
$following[] = $user_to_follow;
}
// retrieve the IDs of all users who are following $user_to_follow
$followers = pwuf_get_followers( $user_to_follow );
if ( $followers && is_array( $followers ) ) {
$followers[] = $user_id;
} else {
$followers = array();
$followers[] = $user_id;
}
do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow );
// update the IDs that this user is following
$followed = update_user_meta( $user_id, '_pwuf_following', $following );
// update the IDs that follow $user_id
$followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers );
// increase the followers count
$followed_count = pwuf_increase_followed_by_count( $user_to_follow ) ;
if ( $followed ) {
do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );
return true;
}
return false;
}
and here to check if a user is following another:
function pwuf_is_following( $user_id, $followed_user ) {
$following = pwuf_get_following( $user_id );
$ret = false; // is not following by default
if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
$ret = true; // is following
}
return $ret;
}
I tried to add this code after updating the user meta but nothing happen!
$subscribers = explode(",", $user_to_follow );
$emails = array ();
foreach ( $subscribers as $subscriber ) {
$user_info = get_userdata($subscriber);
$emails[] = $user_info ->user_email;
}
$body = sprintf( $user_to_follow->display_name, 'followed your work! See <%s>' );
wp_mail( $emails, 'New followers!', $body );
I believe you want to send user an email when ever user is been followed by another user. Given this scenario, there is this action hook available in the plugin which is executed when following process is successful:
do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );
You can hook your own code to this action
add_action('pwuf_post_follow_user', $user_id, $user_to_follow) {
$follower = get_userdata($user_id);
$recipient = get_userdata($user_to_follow);
$recipient_email = $recipient->user_email;
$body = sprintf('%s followed your work!', $follower->display_name );
wp_mail( $recipient_email , 'New follower!', $body );
}
Refernece: https://codex.wordpress.org/Function_Reference/get_userdata

Categories