I’m developing themes for woocommerce, I need help to retrieve information from products by categories, example,
I want to display products from ‘Shirt’ Categories limit by 3 items, here’s the code from woo themes that show products by featured products,( i tried to change to display by categories and not work)
<ul class="featured-products products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$i = 0;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); $_product; $i++;
if ( function_exists( 'get_product' ) ) {
$_product = get_product( $loop->post->ID );
} else {
$_product = new WC_Product( $loop->post->ID );
}
?>
<li class="product <?php if ($i%3==0) echo ' last'; if (($i-1)%3==0) echo ' first'; ?>">
<div class="inner">
<?php woocommerce_show_product_sale_flash( $post, $_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 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" />'; ?>
<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 ); ?>
<?php smpl_product_more_details(); ?>
</div>
</li>
<?php endwhile; ?>
</ul>
I’m new for this,
Thanks in advance
To get featured product by a specific category you just need to use wc_get_products with featured set to true and the category you specify. See below code.
You can see the full tutorial here https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
Replace the $args with the following code
You just need to replace the ENTER_CATEGORY word with the category name you want to display.
Let me know if this fulfills your requirements.
Please use below Query