dougaimian1143 2017-07-23 12:57
浏览 90

从WooCommerce自定义相关产品输出中排除当前产品

The aim is to display 4 products on the product page however remove the current product from the filter. At present I am pulling through products with related brands and categories, however this is also pulling through the current product to the related products...

Current related.php file for Woocommerce contains the following:

if ( ! $related = $product->get_related( $posts_per_page ) ) {
    return;
}
$brands_array = array(0);
$cats_array = array(0);
$cur_product_id = $product->id; 

// get categories
$terms = wp_get_post_terms( $product->id, 'product_brand' );
$category_terms =  wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
    $brands_array[] = $term->term_id;
}
foreach ( $category_terms as $category_term ) {
    $cats_array[] = $category_term->term_id;
}

$final_array = array_merge($brands_array, $cats_array);

$filtered_array = array_filter($final_array, "test_odd");

function test_odd($var)
{
    return($var & 1);
}

var_dump($final_array);

$args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type' => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows' => 1,
    'posts_per_page' => 4,
    'columns' => 4,
    'orderby' => $orderby,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_brand',
            'field' => 'id',
            'terms' => $final_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 echo $filtered_array ?>
        <?php woocommerce_product_loop_start();

             while ( $products->have_posts() ) : $products->the_post();

                    wc_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>

<?php endif;

wp_reset_postdata();

How do I go about filtering the current product from the array of products that are shown on the product page?

Thanks

  • 写回答

1条回答 默认 最新

  • dpgua04022 2017-07-23 15:45
    关注

    To exclude current product, the missing argument in your WP_Query is 'post__not_in' (array). So your $args array is going to be:

    // The current product id (Woocommerce version retro compatible)
    $cur_product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; 
    
    $args = apply_filters( 'woocommerce_related_products_args', array(
        'post_type' => 'product',
        'ignore_sticky_posts' => 1,
        'post__not_in' => array( $cur_product_id ), // <==== HERE
        'no_found_rows' => 1,
        'posts_per_page' => 4,
        'columns' => 4,
        'orderby' => $orderby,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_brand',
                'field' => 'id',
                'terms' => $final_array
            ),
        )
    ));
    
    $products = new WP_Query( $args );
    
    // . . .
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入