Exclude Current Product & Upsells from Custom Related Products WP Query

Question

I’m wondering if anyone can point me in the direction of how to exclude upsells products and current product from Custom Related Products WP Query.

I tried this:

'post__not_in' => get_upsell_ids(), [get_the_ID()]

But not working.

My code:

function eligo_custom_related_products_by_label() {
    
    if (is_product() && has_term('records', 'product_cat')) {

        global $post;

        if ( ! $post ) {
            return;
        }

        $related_by_same_label = wp_get_post_terms($post->ID, 'pa_label', ['fields' => 'slugs']);

        $args = [
            'post_type' => 'product',
            'post_status' => 'publish',
            'orderby' => 'rand',
            'posts_per_page' => 4,
            'post__not_in' => [get_the_ID()],
            'tax_query' => [
                'relation' => 'AND',
                [
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => array('outofstock'),
                    'operator' => 'NOT IN'
                ],
                [
                    'taxonomy' => 'pa_label',
                    'field' => 'slug',
                    'terms' => $related_by_same_label,
                    'operator' => 'IN',
                ],
            ],
        ];

        $loop = new WP_Query($args);

        if ($loop->have_posts()) { ?>

            <section class="related products">
                <div class="related__inner">
                    <?php
                    $attribute_names = ['pa_label'];
                    foreach ($attribute_names as $attribute_name) {
                        $taxonomy = get_taxonomy($attribute_name);
                        if ($taxonomy && !is_wp_error($taxonomy)) {
                            $terms = wp_get_post_terms($post->ID, $attribute_name);
                            $terms_array = [];
                            if (!empty($terms)) {
                                foreach ($terms as $term) {
                                    $archive_link = get_term_link($term->slug, $attribute_name);
                                    $full_line="<a href="" . $archive_link . '">' . $term->name . '</a>';
                                    array_push($terms_array, $full_line);
                                }
                                echo '<h3 class="section__title">' . esc_html__('More from', 'eligo') . ': ' . implode(', ', $terms_array) . '</h3>';
                            }
                        }
                    }

                    woocommerce_product_loop_start(); 

                    while ($loop->have_posts()):
                        $loop->the_post();
                        wc_get_template_part('content', 'product');
                    endwhile;
                    
                    ?>
                </div>
            </section>
            <?php

        } 

        wp_reset_postdata();
    }
}
add_action('woocommerce_after_single_product_summary', 'eligo_custom_related_products_by_label', 20);

Thanks in advance.

0
Ioannis Koukotzilas 6 months 2022-03-03T18:44:29-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse