0

In the related products section, I would like to filter out certain products. I cannot simply hide these products from the shop as I want to use them in the up-sells section.

In the related.php file, is there any argument I can use to filter out certain products? The products I want to filter, all contain the word "set" in them, and they also have a class "setBadge". Can I use these to somehow filter them out? (I cannot use css for this as if I use display: none they leave an empty space, I want to exclude them from the loop).

$args = apply_filters('woocommerce_related_products_args', array(
    'post_type'             => 'product',
    'ignore_sticky_posts'   => 1,
    'no_found_rows'         => 1,
    'posts_per_page'        => $posts_per_page,
    'orderby'               => $orderby,
    'post__in'              => $related,
    'post__not_in'          => array($product->id)
) );

1 Answer 1

0

I couldn't find a simple way to do this without $wpdb but you can try something like this maybe-

$search_query = 'iPhone air pods';
$break_query = explode(' ', $search_query);
$final_params = [];

/* $break_query = array(
    'iphone',
    'air',
    'pods'
); */

foreach( $break_query as $q ) {
    $args = array(
        'key' => '_price',
        'value' => $q,
        'compare' => '<=',
        'type' => 'NUMERIC'
    );

    array_push( $final_params, $q );
}

$params = array(
        'posts_per_page' => 100,
        'post_type' => array('product', 'product_variation'),
        'meta_query' => array(
            'relation' => 'OR',
            $final_params
        )
);

This code haven't been tested but what i did here is that-
I've splitted the main query per word and then I've dynamically created the $params array. The relation is "OR" so if any of these keywords are found, the product will be displayed.

Another option is to use WooCommerce's default search-

<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
    <label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
    <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />
    <input type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" />
    <input type="hidden" name="post_type" value="product" />
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.