PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How can I get/display WooCommerce bestselling products

 May 09, 2022     php, product, woocommerce, wordpress     No comments   

Issue

I am developing a WooCommerce eBook store and I would like to display the list of best seller product on my home page.

How can I do that? can I do it with a custom WP_Query? Any other ways?


Solution

To get Best seller products, you can use different ways:

A WordPress WP_Query (limited here to 20 bestselling products):

$query_args =  array(
   'posts_per_page' =>  20, // -1 (for all)
   'post_type'      =>  array( 'product' ),
   'post_status'    =>  'publish',
   'meta_key'       => 'total_sales',
   'order'          => 'DESC',
   'orderby'        => 'meta_value_num',
);

$query = new WP_Query( $query_args );

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

// Your product data
the_title(); // Output product title

endwhile;
wp_reset_postdata();

else :
// No post found

endif;

A WooCommerce Shordcode (the 20 best selling products):

[products best_selling limit="20"]

Documentation:

  • Woocommerce Shortcodes (since version 3.2)
  • WC_Shortcode_Products set_best_selling_products_query_args() method


Answered By - LoicTheAztec
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing