اطلاعات آماری کاربر با شورتکد [user_stats]
با استفاده از این کد، اطلاعات آماری کاربر در حساب کاربری نمایش داده میشود؛ از جمله تعداد خرید، مجموع مبلغ خرید، سفارشهای تکمیلشده و در انتظار، مدت عضویت، تعداد دیدگاه و نقد و میانگین امتیاز ثبتشده.
<?php
add_shortcode('user_stats', 'gap_user_profile_stats');
function gap_user_profile_stats() {
if (!is_user_logged_in()) {
return '<div class="user-stats-wrapper">لطفاً وارد حساب کاربری خود شوید.</div>';
}
$user_id = get_current_user_id();
$order_count = wc_get_customer_order_count($user_id);
$total_spent = wc_get_customer_total_spent($user_id);
$formatted_total_spent = number_format($total_spent, 0, '.', ',') . ' تومان';
$completed_count = count(wc_get_orders(array('customer' => $user_id, 'status' => 'completed', 'return' => 'ids')));
$pending_count = count(wc_get_orders(array('customer' => $user_id, 'status' => 'pending', 'return' => 'ids')));
$user_data = get_userdata($user_id);
$date_start = new DateTime($user_data->user_registered);
$interval = $date_start->diff(new DateTime());
$total_months = ($interval->y * 12) + $interval->m;
$comments_count = get_comments(array('user_id' => $user_id, 'count' => true, 'comment_type' => 'comment'));
$reviews_count = get_comments(array('user_id' => $user_id, 'count' => true, 'comment_type' => 'review'));
$reviews = get_comments(array('user_id' => $user_id, 'status' => 'approve', 'type' => 'review'));
$total_rating = 0; $rating_count = 0;
foreach ($reviews as $review) {
$rating = get_comment_meta($review->comment_ID, 'rating', true);
if ($rating) { $total_rating += intval($rating); $rating_count++; }
}
$avg_rating = $rating_count ? round($total_rating / $rating_count, 1) : 0;
ob_start();
?>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%40font-face%20%7B%0A%20%20%20%20font-family%3A%20'IRANYekan'%3B%0A%20%20%20%20src%3A%20url('https%3A%2F%2Fgolds.ir%2Fwp-content%2Fthemes%2Fhello-child%2Ffont%2Firanyekanwebregular.ttf')%20format('truetype')%3B%0A%7D%0A.user-stats-wrapper%20%7B%20display%3A%20grid%3B%20grid-template-columns%3A%20repeat(8%2C%201fr)%3B%20gap%3A%205px%3B%20direction%3A%20rtl%3B%20font-family%3A%20'IRANYekan'%2C%20sans-serif%3B%20%7D%0A.user-stat-box%20%7B%20background%3A%20%23f5f5f5%3B%20border-radius%3A%205px%3B%20padding%3A%205px%3B%20border%3A%201px%20solid%20%23e0e0e0%3B%20display%3A%20flex%3B%20justify-content%3A%20space-between%3B%20align-items%3A%20center%3B%20%7D%0A.user-stat-label%20%7B%20font-size%3A%2010px%3B%20color%3A%20%23666%3B%20white-space%3A%20nowrap%3B%20%7D%0A.user-stat-value%20%7B%20font-size%3A%2011px%3B%20font-weight%3A%20bold%3B%20color%3A%20%23111%3B%20margin-right%3A%205px%3B%20%7D%0A%40media%20(max-width%3A%20900px)%20%7B%20.user-stats-wrapper%20%7B%20grid-template-columns%3A%20repeat(4%2C%201fr)%3B%20%7D%20%7D%0A%40media%20(max-width%3A%20600px)%20%7B%20.user-stats-wrapper%20%7B%20grid-template-columns%3A%20repeat(2%2C%201fr)%3B%20%7D%20%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-style" width="20" height="20" alt="<style>" />
<div class="user-stats-wrapper">
<div class="user-stat-box"><span class="user-stat-label">خرید</span><span class="user-stat-value"><?php echo $order_count; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">مبلغ</span><span class="user-stat-value"><?php echo $formatted_total_spent; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">تکمیل</span><span class="user-stat-value"><?php echo $completed_count; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">انتظار</span><span class="user-stat-value"><?php echo $pending_count; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">عضویت</span><span class="user-stat-value"><?php echo $total_months; ?> م</span></div>
<div class="user-stat-box"><span class="user-stat-label">دیدگاه</span><span class="user-stat-value"><?php echo $comments_count; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">نقد</span><span class="user-stat-value"><?php echo $reviews_count; ?></span></div>
<div class="user-stat-box"><span class="user-stat-label">امتیاز</span><span class="user-stat-value"><?php echo $avg_rating; ?></span></div>
</div>
<?php
return ob_get_clean();
}