Exclude current product from WooCommerce products you release

The goal is to display 4 products on the product page, however remove the current product from the filter. I am currently looping through products with related brands and categories, however this also pulls through the current product towards related products ...

The 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 choose to filter the current product from the array of products displayed on the product page?

thank

+3


source to share


1 answer


To exclude the current product, the missing argument in - . So your $ args array would be: WP_Query

'post__not_in' (array)



// 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 );

// . . .

      

0


source







All Articles