KostasS
KostasS

Reputation: 41

Hide woocommerce products that are lower than a specified price

I would like to add custom args to all the Woocommerce Product queries to make them return only the products that are above a specified price. (>10$)

I would like to use an action on my theme's function.php file.

Any clue? Thank you very much!

Upvotes: 1

Views: 1320

Answers (2)

kirillrocks
kirillrocks

Reputation: 223

Add this function to the bottom of your functions.php and change the number to the correct one.

Hooking to woocommerce_product_query directly.

add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_zero' );
function react2wp_hide_products_higher_than_zero( $q ){
   $meta_query = $q->get( 'meta_query' );
   $meta_query[] = array(
      'key'       => '_price',
      'value'     => 10, // CHANGE THIS TO THE NUMBER YOU WANT
      'compare'   => '>'
   );
   $q->set( 'meta_query', $meta_query );
}

Taken from: https://react2wp.com/woocommerce-hide-products-without-price-simple-fix/

Upvotes: 1

glutaminefree
glutaminefree

Reputation: 382

add_action('pre_get_posts', function($query){
    if ( !is_admin() and is_product_category() and $query->is_main_query() ) {
        $meta_query = $query->get('meta_query');
        $meta_query['relation'] = 'AND';

        $meta_query[] = [
            'key'   => '_price',
            'value' => 100,
            'compare' => '<',
            'type' => 'NUMERIC',
        ];

        $query->set('meta_query', $meta_query);
    }
});

Upvotes: 1

Related Questions