Hide products with zero price

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 );
}

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

Click to Copy