In a simple product, the stock status is only displayed when you change it to anything that is not 'In stock'. Is it possible to always display the stock status?
I tried using this code:
add_filter( 'woocommerce_get_availability', 'custom_override_get_availability', 10, 2);
// The hook in function $availability is passed via the filter!
function custom_override_get_availability( $availability, $_product ) {
if ( $_product->is_in_stock() ) $availability['availability'] = __('In Stock', 'woocommerce');
return $availability;
}
But this overwrites my custom stock status text for some reason.
To always show the stock status in WooCommerce, without overwriting your custom status use the following:
add_filter( 'woocommerce_get_availability', 'filter_product_get_availability', 10, 2);
function filter_product_get_availability( $availability, $product ) {
if ( $product->get_stock_status() === 'instock' ) {
$availability['availability'] = wc_format_stock_for_display( $product );
} elseif ( $product->managing_stock() && $product->is_on_backorder( 1 )
&& ! $product->backorders_require_notification() ) {
$availability['availability'] = __('In Stock', 'woocommerce');
$availability['class'] = 'in-stock';
}
return $availability;
}
Code goes in functions.php file of your child theme (or in a plugin). It should work without making trouble with your custom status.
Explanations:
The product conditional method is_in_stock()
used in your IF
statement is like using:
if ( 'outofstock' !== $product->get_stock_status() ) {
So it doesn't handle other product stock statuses, especially custom ones.
See it in the source code for WC_Product is_in_stock()
method.