Categories
Techie stuff WooCommerce

WooCommerce: Custom “Out of Stock” Text

I’ve used a short snippet of PHP code within my child theme in WordPress to change the text displayed (in the several places it does) when a product is out of stock – to something else. On our eCommerce shop, we wanted it to simply say “Sold Out” as we sell magazines, and once the stock has gone, it isn’t replaced. We then later trash the product – and if you need a bulk delete function using SKU’s I’ve got some SQL code to help with that.

Anyway, to use the PHP snippets below – I’d suggest you use a child theme, and edit the functions.php file in the child theme (not the main theme!). If you don’t use a child them, there are plugins which let you paste in PHP code to be run as if it’s in the functions.php file.

add_filter( 'woocommerce_get_availability', 'db_custom_get_availability', 1, 2);
function db_custom_get_availability( $availability, $_product ) {

  // Change Out of Stock Text
  if ( ! $_product->is_in_stock() ) {
     $availability['availability'] = __('SOLD OUT', 'woocommerce');
  }
 return $availability;
}

add_filter('gettext', 'translate_text');
add_filter('ngettext', 'translate_text');

function translate_text($translated) {
  $translated = str_ireplace('Out of Stock', 'Sold Out', $translated);
  return $translated;
}

By Krispy Brown

Developer (Java / PHP / MS Business Central AL).
Beer brewer and drinker.
DIY Punk promoter.