簡體   English   中英

如何使用 wp_query woocommerce 上周和上個月獲得最暢銷的產品

[英]How to get best selling products using wp_query woocommerce last week and last month

我想按上周和上個月對主頁上的暢銷產品進行排序。 我正在使用下面的查詢但這並沒有獲得所有類別的暢銷產品。

function get_product_posts_hp(){
    $query = new WP_Query( array(
        'posts_per_page' => 12,
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    ) );

    echo '<p>Count: '. $query->post_count ; '</p>';

    if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();

            echo '<p>' . get_the_title() . ' (';
            echo get_post_meta( get_the_id(), 'total_sales', true) . ')</p>';

        endwhile;
        wp_reset_postdata();
    endif;
}

您的查詢不是獲得此信息的最佳方式,因為您希望有一段時間。 您必須使用帖子類型 shop_orders 獲取特定時間段內狀態為完整的所有訂單,然后將訂單中的訂單項目分解為項目,您將獲得一段時間內最暢銷的產品。 或者,如果您不想要一段時間,那么您可以將其添加到您的查詢中

    $query = new WP_Query( array(
    'posts_per_page' => 12,
    'post_type' => 'product',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query'=> array(
    array(
      'key' => 'total_sales',
      'compare' => '>=',
      'value' => 1,
      'type' => 'numeric',
    )
  )
) );

您可以根據價值更改此產品至少必須具有的總銷售額:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM