Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Hide the "In stock" message on product page.
<?php
/**
* Hide the "In stock" message on product page.
*
* @param string $html
* @param string $text
* @param WC_Product $product
* @return string
*/
function my_wc_hide_in_stock_message( $html, $text, $product ) {
$availability = $product->get_availability();
if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] ) {
return '';
}
return $html;
}
add_filter( 'woocommerce_stock_html', 'my_wc_hide_in_stock_message', 10, 3 );
@skoldin

This comment has been minimized.

Copy link

skoldin commented Jul 25, 2018

In 2018, it can be a bit simpler:

function my_wc_hide_in_stock_message( $html, $product ) {
	if ( $product->is_in_stock() ) {
		return '';
	}

	return $html;
}

add_filter( 'woocommerce_get_stock_html', 'my_wc_hide_in_stock_message', 10, 2 );
@underthegui

This comment has been minimized.

Copy link

underthegui commented Jul 26, 2018

This would remove the stock being shown every time wc_get_stock_html gets called. In my case I wanted to call wc_get_stock_html in another part of the product page, so I had to overwrite the template entirely...

It can be overridden by adding the template here: yourtheme/woocommerce/single-product/add-to-cart/simple.php.

If there's a better way, let me know!

@theodiablo

This comment has been minimized.

Copy link

theodiablo commented Oct 3, 2018

Otherwise, you can simply remove it from this position using the wonderful filter provided by @skoldin, and then insert your other block wherever you want using this kind of ations, playing with the priority to put it where you want:

add_action('woocommerce_single_product_summary', 'add_my_new_block', 20);

function add_my_new_block() {
    global $product;
    if($product->is_in_stock( )) {
       echo <p>My Foo Content!!!</p>;
    }
}

This avoids having to update your templates everytime there is a woocommerce update ;-)

@DayleySenior

This comment has been minimized.

Copy link

DayleySenior commented Dec 12, 2018

@skoldin
Does this also remove the "not in stock" as well as the "in stock"? And is there a "low stock" (Or something similar) in WooCommerce also?

@DayleySenior

This comment has been minimized.

Copy link

DayleySenior commented Dec 14, 2018

Nevermind. This should solve my problem:

add_filter( 'woocommerce_get_stock_html', '__return_empty_string', 10, 2 );

@Felipe-NS

This comment has been minimized.

Copy link

Felipe-NS commented Jan 13, 2019

Here is hiding the in stock and also the low threhold stock message. Is there a way to hide just the in stock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.