Site icon Qaisar Satti's Blogs

WooCommerce Get Top Seller Category Product

Today we are going to talk about how to get a top seller category product in WooCommerceor wordpress. This tutorial will guide you to place your top seller product by category in a single place. It will also help you to display your top seller category product on the listing page.

Below is an example:

$queryargs = array(
               'post_type' => 'product',
                'meta_key' => 'total_sales',
                'orderby' => 'meta_value_num',
            'order' => 'DESC',
                'post_status' => 'publish',
                'posts_per_page' => 10,
          'tax_query'             => array(
    array(
      'taxonomy'      => 'product_cat',
      'field' => 'term_id', //This is optional, as it defaults to 'term_id'
      'terms'         => $category->term_id,
      'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
      )
      ),
          );
$query = new WP_Query( $queryargs );
while ( $query->have_posts() ) {           
        $query->the_post();            
        $totalSales =   get_metadata( 'post', $query->post->ID, 'total_sales', true );
        if($totalSales){         
          if($totalSales[0] > 0) {       
              echo the_title();
          }        
      }      
}

Here is an available free WooCommerce top seller category product extension.

Exit mobile version