add_filter( 'woocommerce_get_price_html', 'wpcb_hide_price_on_sold_out', PHP_INT_MAX, 2 ); if ( ! function_exists( 'wpcb_hide_price_on_sold_out' ) ) { /** * wpcb_hide_price_on_sold_out. * * @see https://wpcodebook.com/snippets/hide-price-for-sold-out-products-in-woocommerce/ */ function wpcb_hide_price_on_sold_out( $price_html, $product ) { return ( ! $product->is_in_stock() ? '' : $price_html ); } }
The function checks if the current product is in stock; if it’s not in stock (i.e., not instock or onbackorder) – returns an empty string; otherwise – returns unchanged price html.