Welcartでは売上ランキングを作成するための商品ID取得用関数 get_bestseller_ids( $days ) と売上ランキング表示用関数 usces_list_bestseller( $num, $days ) が用意されています。
売上ランキング用 商品ID取得関数
/**
 * Welcart 売上ランキング用 商品ID取得関数
 * \wp-content\plugins\usc-e-shop\classes\usceshop.class.php
 * 7403行目
 */
function get_bestseller_ids( $days = "" )
{
    global $wpdb;
    $datestr = substr(get_date_from_gmt(gmdate('Y-m-d H:i:s', time())), 0, 10);
    $yearstr = substr($datestr, 0, 4);
    $monthstr = substr($datestr, 5, 2);
    $daystr = substr($datestr, 8, 2);
    $res = array();
    $order_table_name = $wpdb->prefix . "usces_order";
    $where = "";
    if( empty($days) ){
        $days = 30;
    }
    $order_date = date('Y-m-d H:i:s', mktime(0, 0, 0, (int)$monthstr, ((int)$daystr-$days), (int)$yearstr));
    $where = " WHERE order_date >= '{$order_date}'";
    $query = "SELECT order_cart FROM {$order_table_name}" . $where;
    $dbres = $wpdb->get_col($query);
    if(!$dbres) return false;
    
    foreach((array)$dbres as $carts){
        $rows = unserialize($carts);
        foreach((array)$rows as $carts){
            if( 'publish' != get_post_status($carts['post_id']) )
                continue;
                
            $id = $carts['post_id'];
            $qu = $carts['quantity'];
            if(array_key_exists($id, $res)){
                $res[$id] = $res[$id] + $qu;
            }else{
                $res[$id] = $qu;
            }
        }
    }
    arsort($res);
    $results = array_keys($res);
    return $results;
}売上ランキング表示用関数
usces_list_bestseller( $num, $days ) の第1引数にランキングの件数(10位まで表示するなら10)、第2引数に集計日数(集計対象の過去何日分かの数字を指定する7日間であれば 7)を指定してランキングを取得&表示します。
/**
 * Welcart 売上ランキング表示用関数
 * \wp-content\plugins\usc-e-shop\functions\template_func.php
 * 1791行目
 */
function usces_list_bestseller($num, $days = '')
{
    global $usces;
    $ids = $usces->get_bestseller_ids( $days );
    $htm = "";
    for($i=0; $i<$num; $i++){
        if(isset($ids[$i])){
            $post = get_post($ids[$i]);
            if( !is_object($post) )
                continue;
                
            $disp_text = apply_filters('usces_widget_bestseller_auto_text', esc_html($post->post_title), $ids[$i]);
            $list = "<li><a href='" . get_permalink($ids[$i]) . "'>" . $disp_text . "</a></li>\n";
            $htm .= apply_filters('usces_filter_bestseller', $list, $ids[$i], $i);
        }
    }
    wp_reset_postdata();
    echo $htm;
}独自のフォーマットでランキングを表示したい場合
usces_list_bestseller( $num, $days ) を参考に独自に関数を定義して対応します。
以下2行のコードで売上ランキング用の商品ID(配列)を取得することが出来ます。
商品ID取得
global $usces;
$ids = $usces->get_bestseller_ids( $days );商品ID取得結果出力(配列)
array(2) {
  [0]=>int(9)
  [1]=>int(64)
}動作環境情報
エックスサーバー(x10) PHP 7.3.14 MySQL 5.7 WordPress 5.3.2 Welcart e-Commerce 1.9.28

 
  
  
  
  
コメント