Hide products with zero price

Davor Mlinarić
18 Jun 2022

Just a simple function that will hide products that have price set to 0. I found it useful when I was importing products from some external source where client decided to add prices manually.

add_action( 'woocommerce_product_query', 'wb_hide_products_with_zero_price' );
function wb_hide_products_with_zero_price( $q ){
   $meta_query = $q->get( 'meta_query' );
   $meta_query[] = array(
      'key'       => '_price',
      'value'     => 0,
      'compare'   => '>'
   );
   $q->set( 'meta_query', $meta_query );
}

In case that there is no price set to 0, where price is empty this snippet could be used.

add_action( 'woocommerce_product_query', 'wb_hide_products_without_price' );
function wb_hide_products_without_price( $q ){
   $meta_query = $q->get( 'meta_query' );
   $meta_query[] = array(
      'key'       => '_price',
      'value'     => '',
      'compare'   => '!='
   );
   $q->set( 'meta_query', $meta_query );
}

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy