我无法排除相关产品Woocommerce中的当前产品, 现在我已将相关产品从类别更改为子类别(无子项)。因此,相关产品将仅显示子类别产品(相同子类别),但现在它还将显示当前产品。任何人都有解决方案>谢谢
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
$args = apply_filters( 'woocommerce_related_products_args', array(
'exclude' => array( $cur_product_id ),
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $cur_product_id ),
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
答案 0 :(得分:0)
你的问题是$ cur_product_id变量! 改变:
$product->get_the_id()
答案 1 :(得分:0)
非常感谢你。我找到了解决方案
添加此
$ terms = wp_get_post_terms($ product-&gt; id,'product_cat');
这个'post__not_in'=&gt;数组($ cur_product_id),
这是代码
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// The current product id (Woocommerce version retro compatible)
$cur_product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
$args = apply_filters( 'woocommerce_related_products_args', array(
'exclude' => array( $cur_product_id ),
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $cur_product_id ),
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();