Alexis K
Alexis K

Reputation: 3

Woocommerce - hide product price when out of stock (not in admin)

I'm trying to remove product prices from woocommerce out of stock products.

I've used this so far (in the functions.php) :

add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock()) {$price = '';}
return $price;
}

The problem is that it's also hiding the price in the orders admin list. How can I avoid that ?

Thanks !

Upvotes: 0

Views: 441

Answers (1)

bhanu
bhanu

Reputation: 1860

Just check for it.

add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );

function remove_price_ofs( $price, $product ) {
   if ( ! $product->is_in_stock() && (is_product() || is_product_category() || is_shop() ) ) {$price = '';}
   return $price;
}

OPTION 2

To run only on frontend.

add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );

function remove_price_ofs( $price, $product ) {
   if ( ! $product->is_in_stock() && ! is_admin() ) {$price = '';}
   return $price;
}

We just check if its a product page and if it is then only we change the price. You might want to do this for archive pages as well.

List of conditional tags you can use. https://docs.woocommerce.com/document/conditional-tags/

Upvotes: 0

Related Questions