Woocommerce Get Featured Products

Woocommerce Get Featured Products

I’m writing this post to clear things up a bit regarding this subject. my post contains a code snippet used to get featured products in Woocommerce. Keep in mind that in order to use this snippet effectively you probably need to be a  developer.
this feature allows us store owners to promote specific products of our choosing. Getting this job done if fairly easy, there is nothing fancy here. However, we need to do this right. You should do this in the following way.

How can I programmatically accomplish this in Woocommerce?

function codelikepro_featured_products() {
   $args = array(
      'post_type'      => 'product',
      'posts_per_page' => 16,
      'orderby'        => 'desc',
      'order'          => 'desc',
      'post__in'       => wc_get_featured_product_ids(),
   );
   $query = new WP_Query( $args );
   $output = '<div class="row sm_products_grid js_tab_row">';
   ob_start();
   if ( $query->have_posts() ) {
      while ( $query->have_posts() ) {
         $query->the_post();
         wc_get_template_part( 'content', 'product' );
      }
   }
   $output .= '</div>';
   return $output;
}

Let me explain what’s going on here:

I’ll start by telling you that this function comes with a twist. In addition to getting featured products using WordPress WP Query, I’m also getting the template part for the product. Before running the query I set up my arguments and then run it.

To sum this up

In conclusion, we supply the right args for wp query, run it, that’s it. However, if you have a question, feel free to ask in the comment section below!
More Snippets here.
Woocommerce Documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *