Reputation: 1180
I would like to fetch WooCommerce product on the basis of product category on my page template.
Lets say I have a category mattress
. I want to fetch all the products related to that mattress
category.
Here is my code:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
How can I achieve this?
Thanks.
Upvotes: 0
Views: 4209
Reputation: 19
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'title';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
$category_id = $cat->term_id;?>
<li><a href="<?php echo get_term_link($cat->slug, 'product_cat');?>"><?php echo $cat->name;?></a></li>
<?php }?>
</ul>
Upvotes: 1
Reputation: 253784
— — ( Update 2 of the August 8th ) — —
Finally a good alternative is to use has_term();
wordpress function to filter all products of some category in an if
statement. If you have multiple categories you can embed them in an array using also has_term();
function.
As I have said before below on a comment, I think that your problem is here:
// Include the page content template. get_template_part( 'content', 'page' );
So I will replace it with some custom code to show you that the condition is working fine:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
// Start the Loop
If($loop->have_posts()){
while ( $loop->have_posts() ) : $loop->the_post();
// Here it is your product category
if ( has_term( 'mattress', 'product_cat', $loop->post ); ) {
// from here I display products thumbnails and name
echo '<div class="woocommerce-product" style="padding:5px; float:left;">';
if (has_post_thumbnail( $product_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" />';
}
echo '<div class="product-name">' . $loop->post->post_name . '</div>';
echo '</div>';
}
endwhile;
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
It also works without condition: has_term( 'mattress', 'product_cat', $loop->post );
replacing $args
array by this one:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
This code is tested and fully functional and it goes on function.php file of your active child theme or theme.
Reference:
— — ( Update 1 - july 9th ) — —
Or you can also use has_category();
function to filter in an if
statement this way:
<?php
global $post;
// Start the Loop
while ( have_posts() ) : the_post();
if ( has_category( 'mattress', $post ); ) {
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
endwhile;
?>
You could try this:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
$loop = new WP_Query($args);
If($loop->have_posts()){
while($loop->have_posts()) {
$loop->the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
Upvotes: 2
Reputation: 707
Below code should work.
<?php
$query = new WP_Query('posts_per_page=5&post_type=product&product_cat=mattress');
If($query->have_posts()){
while($query->have_posts()) {
$query->the_post();
// Your Code
}
}
wp_reset_query();
wp_reset_postdata();
?>
Upvotes: 1