Get WooCommerce products from specific category



PHP Snippet 1:

$prod_categories = array(1, 2,3);
$product_args = array(
  'numberposts' => $limit,
  'post_status' => array('publish', 'pending', 'private', 'draft'),
  'post_type' => array('product', 'product_variation'),
  'orderby' => 'ID',
  'suppress_filters' => false,
  'order' => 'ASC',
  'offset' => 0
);

if (!empty($prod_categories)) {
   $product_args['tax_query'] = array(
      array(
        'taxonomy' => 'product_cat',
        'field' => 'id',
        'terms' => $prod_categories,
        'operator' => 'IN',
   ));
 }

 $products = get_posts($product_args);

PHP Snippet 2:

<?php
/**
* Template Name: Courses template
*
*/
defined( 'ABSPATH' ) || exit;


get_header();
?>
    <div id="content" class="content" role="main">

        <ul class="products">
            <?php
                $args = array( 
                    'post_type' => 'product', 
                    'posts_per_page' => -1, 
                    'product_cat' => 'clothing', // Category slug "clothing"
                    'orderby' => 'rand' 
                );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
            
                    <h2>Courses</h2>
            
                        <li class="product">    
            
                            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
            
                                <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
            
                                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
            
                                <h3><?php the_title(); ?></h3>
            
                                <span class="price"><?php echo $product->get_price_html(); ?></span>                    
            
                            </a>
            
                            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
            
                        </li>
            
            <?php endwhile; ?>
            <?php wp_reset_query(); ?>
        </ul>
    </div><!-- #content -->

<?php get_footer(); ?>

PHP Snippet 3:

 <?php
    $product_term_slugs = array('shirts');
    
    $product_args = array(
        'post_status' => 'publish',
        'limit' => -1,
        'category' => $product_term_slugs,
        //more options according to wc_get_products() docs
    );
    $products = wc_get_products($product_args);
   ?>

PHP Snippet 4:

 $product_term_ids = array(12);
    
    $product_term_args = array(
        'taxonomy' => 'product_cat',
        'include' => $product_term_ids,
        'orderby'  => 'include'
    );
    $product_terms = get_terms($product_term_args);
    
    $product_term_slugs = [];
    foreach ($product_terms as $product_term) {
        $product_term_slugs[] = $product_term->slug;
    }
    
    $product_args = array(
        'post_status' => 'publish',
        'limit' => -1,
        'category' => $product_term_slugs,
        //more options according to wc_get_products() docs
    );
    $products = wc_get_products($product_args);
  

PHP Snippet 5:

<?php foreach ($products as $product) {
 $product_id = $product->get_id();
 $product_type = $product->get_type();
 $product_title = $product->get_title();
 $product_permalink = $product->get_permalink();
 $product_regular_price = $product->get_regular_price();
 $product_sale_price = $product->get_sale_price();
 $product_short_desc = $product->get_short_description();
 $product_categories = $product->get_categories(); 
 ?>
 <!-- display product html -->
 <?php } ?>