user3186420
user3186420

Reputation: 41

Exclude the current product from WooCommerce custom Related Products output

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

Upvotes: 4

Views: 1813

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254373

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

$args = apply_filters( 'woocommerce_product_related_posts_query', array(
    'post_type' => 'product',
    'ignore_sticky_posts' => 1,
    'post__not_in' => array( $product->get_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 );

// . . .

See: woocommerce_product_related_posts_query replace woocommerce_related_products_args since WooCommerce 3+ (thanks to @strarsis).

Upvotes: 1

Related Questions