admin管理员组文章数量:1315225
I have modified my single-product.php and want to pop in a block of other products that share the same brand name which I have specified in a product attribute called 'brand'.
This is my code so far but it is returning all the products and NOT filtering by the pa_brand.
<!-- Custom 4 up product filtered by Brand attribute -->
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $product->get_attribute( 'brand' )
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!–/.products–>
I have modified my single-product.php and want to pop in a block of other products that share the same brand name which I have specified in a product attribute called 'brand'.
This is my code so far but it is returning all the products and NOT filtering by the pa_brand.
<!-- Custom 4 up product filtered by Brand attribute -->
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $product->get_attribute( 'brand' )
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!–/.products–>
Share
Improve this question
asked Nov 19, 2020 at 0:32
Marney FontanaMarney Fontana
31 bronze badge
1
|
1 Answer
Reset to default 0So as @shanebp suggested you have to use tax_query it would also help if you use operator in tax_query. following is your code after make some minor changes;
<ul class="products-newo">
<?php
$attributes = $product->get_attributes();
$pa_brand = $attributes["pa_brand"];
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'tax_query' => array( array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $pa_brand->get_slugs(),
'operator' => 'IN',
) )
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul>
But i also want to suggest if you check a small condition just after the $attributes = $product->get_attributes(); line.
if( isset($attributes["pa_brand"]) )
Otherwise if any product does not have this value you will get error.
Thank you.
本文标签: woocommerce offtopicReturn product list based on current product attribute
版权声明:本文标题:woocommerce offtopic - Return product list based on current product attribute 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741981838a2408441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tax_query
. developer.wordpress/reference/classes/wp_query/… – shanebp Commented Nov 19, 2020 at 2:34