I’ve been trying to get rid of our exact stock numbers on product pages on our wordpress/woocommerce website. Ideally it shows In stock, Only 1 or 2 left in stock when theres only 1 or 2 available, or just sold out. I’ve tried many of the code snippets found here on stack overflow but none of them seem to change anything. Also the standard woocommerce settings at Products > Inventory to don’t show any stock numbers doesn’t do anything. On the front end it keeps showing Availability: Exact stock amount. Can anyone help me out?
Adding these kind of snippets to my child theme’s functions.php or even in a code snippet plugin doesn’t seem to do anything for me:
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}
2
Answers
A better way is to use
woocommerce_get_availability_text
instead ofwoocommerce_get_availability
. This filters only the availability text and is called afterwoocommerce_get_availability
.If the above code doesn’t do anything, test if the filter is being called by using the
die()
function. This will stop the execution of any code that comes after it and prints whatever string you put in between the parentheses:So if the filter is called correctly your page should render up to the availabitly text and display ‘Filter is called’ where the availabilty text should be. If nothing happens this means the filter isn’t called. So then check if your functions.php is being loaded, or use a plugin like Code Snippets.
This snippet help you to change text for all product or particular one: