Skip to main content
deleted 24 characters in body; edited tags; edited title
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

Exclude Current Product & Upsells from Custom Related Products WP Query Combine 2 arrays in a query parameter

I'm wondering if anyone can point me in the direction of howtrying to exclude upsells productsusing post__not_in but I already have a value and current product from Custom Related Products WP Querydo not know how to combine the two.

But it is not working.

Exclude Current Product & Upsells from Custom Related Products WP Query

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.

But not working.

Combine 2 arrays in a query parameter

I'm trying to exclude upsells using post__not_in but I already have a value and do not know how to combine the two.

But it is not working.

Source Link

Exclude Current Product & Upsells from Custom Related Products WP Query

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.