Deprecated: Function seems_utf8 is deprecated since version 6.9.0! Use wp_is_valid_utf8() instead. in /home/dplugins-code/htdocs/code.dplugins.com/wp-includes/functions.php on line 6131

Deprecated: Function seems_utf8 is deprecated since version 6.9.0! Use wp_is_valid_utf8() instead. in /home/dplugins-code/htdocs/code.dplugins.com/wp-includes/functions.php on line 6131
Hide products with zero price – Code DPlugins

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 );
}
Click to Copy