How to Hide WooCommerce Product If Price Is Zero?

Want to Hide Products, When Price is Zero ?

When the Product is not available OR out of stock then many store owners make the price ” O ” , It gives an expression to the user that Product is not available.

I have added two products, one of the has price zero though both products one with non-zero price and other having price zero are shown in store.

  • Go to Dashboard -> Appearance -> Editor
  • Click on functions.php
  • Add the following Code in the end of the functions file

[codesyntax lang=”php”]

 Enter code here//Put this code in functions.php file
add_action( 'woocommerce_product_query', 'themelocation_product_query' );
function themelocation_product_query( $q ){
    $meta_query = $q->get( 'meta_query' );
        $meta_query[] = array(
                    'key'       => '_price',
                    'value'     => 0,
                    'compare'   => '>'
                );
    $q->set( 'meta_query', $meta_query );
}

[/codesyntax]

Click Update to Save the file.

Open your shop page you can see the product with price zero is not shown in the products.

That’s All.

 

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

2 thoughts on “How to Hide WooCommerce Product If Price Is Zero?”

  1. Thank you! I spent 3 days looking for a similar solution on the Russian Internet. Found only on your site. Thank you. All products with zero prices are gone.

  2. This didn’t work for me on Woo v.4.5.1, but I found out what was missing! 🙂

    Here’s the full snippet I added:

    /**
    * @snippet Remove any products from front-end shop pages if the price is $0.00
    * @how-to https://www.themelocation.com/hide-woocommerce-product-if-price-zero/, https://wordpress.org/support/topic/hiding-woocommerce-products-higher-than-a-certain-price/
    * @testedwith Woo 4.5.1
    */
    add_action( ‘woocommerce_product_query’, ‘themelocation_product_query’ );
    function themelocation_product_query( $q ){
    $meta_query = $q->get( ‘meta_query’ );
    $meta_query[] = array(
    ‘key’ => ‘_price’,
    ‘value’ => 0,
    ‘compare’ => ‘>’,
    ‘type’ => ‘NUMERIC’ //ensures the comparison isn’t evaluated as a string
    );
    $q->set( ‘meta_query’, $meta_query );
    }

Leave a Comment

Your email address will not be published. Required fields are marked *