Untitled
webwizards
plain_text
9 months ago
3.8 kB
13
Indexable
add_action('marketking_after_vendor_product_page_text', function(){
// Static flag to ensure this only runs once per page load
static $stats_displayed = false;
if ($stats_displayed) {
return; // Exit if already displayed
}
// Don't display if we're in a loop (e.g., product listings, archives)
if (in_the_loop()) {
return;
}
// Get current product and vendor info
global $post;
$product = wc_get_product($post->ID);
if (!$product) return;
$vendor_id = marketking()->get_product_vendor($product->get_id());
if (empty($vendor_id)) return;
// 1. GET NUMBER OF FOLLOWERS (with caching)
$followers_count = get_transient('marketking_vendor_followers_' . $vendor_id);
if ($followers_count === false) {
// Query all users who follow this vendor
$args = array(
'meta_key' => 'marketking_follows_vendor_' . $vendor_id,
'meta_value' => 'yes',
'fields' => 'ID',
'number' => -1
);
$followers = get_users($args);
$followers_count = count($followers);
// Cache for 24 hours
set_transient('marketking_vendor_followers_' . $vendor_id, $followers_count, DAY_IN_SECONDS);
}
// 2. GET NUMBER OF SOLD PRODUCTS (with caching)
$sold_products_count = get_transient('marketking_vendor_sold_products_' . $vendor_id);
if ($sold_products_count === false) {
$sold_products_count = 0;
$vendor_orders = marketking()->get_vendor_orders($vendor_id);
foreach ($vendor_orders as $order) {
$order_obj = wc_get_order($order);
if ($order_obj && in_array($order_obj->get_status(), array('completed', 'processing'))) {
foreach ($order_obj->get_items() as $item) {
$sold_products_count += $item->get_quantity();
}
}
}
// Cache for 24 hours
set_transient('marketking_vendor_sold_products_' . $vendor_id, $sold_products_count, DAY_IN_SECONDS);
}
// 3. GET ACTIVE PRODUCTS (purchasable products)
$active_products_count = get_transient('marketking_vendor_active_products_' . $vendor_id);
if ($active_products_count === false) {
$active_products_count = wc_get_products(array(
'numberposts' => -1,
'post_status' => 'publish',
'author' => $vendor_id,
'stock_status' => array('instock', 'onbackorder'), // Only in-stock or backorderable
'return' => 'ids'
));
$active_products_count = count($active_products_count);
// Cache for 24 hours
set_transient('marketking_vendor_active_products_' . $vendor_id, $active_products_count, DAY_IN_SECONDS);
}
// Display the vendor stats
?>
<div class="marketking-vendor-stats" style="margin-top: 10px; padding: 12px 15px; background: #f8f9fa; border: 1px solid #e0e0e0; border-radius: 4px; display: inline-block;">
<span style="margin-right: 15px; font-size: 14px;">
<strong><?php echo esc_html($followers_count); ?></strong> <?php echo _n('Follower', 'Followers', $followers_count, 'marketking'); ?>
</span>
<span style="margin-right: 15px; font-size: 14px;">
<strong><?php echo esc_html($sold_products_count); ?></strong> <?php echo __('Sold Products', 'marketking'); ?>
</span>
<span style="font-size: 14px;">
<strong><?php echo esc_html($active_products_count); ?></strong> <?php echo __('Active Products', 'marketking'); ?>
</span>
</div>
<?php
// Mark as displayed
$stats_displayed = true;
});Editor is loading...
Leave a Comment