Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce: Changing the "# in stock" text

I need to change the "# in stock" text to "# deals left".

I added the following code to the function.php file but this removes the actual number.

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
//change text "In Stock' to 'SPECIAL ORDER'
if ( $_product->is_in_stock() ) $availability['availability'] = __('SPOTS LEFT', 'woocommerce');

//change text "Out of Stock' to 'SOLD OUT'
if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');
    return $availability;
}

Can anybody help with this?

like image 741
Lino Meert Avatar asked Oct 12 '25 07:10

Lino Meert


2 Answers

you can try below code:

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
  global $product;
  $stock = $product->get_total_stock();

  if ( $_product->is_in_stock() ) $availability['availability'] = __($stock . ' SPOTS LEFT', 'woocommerce');
  if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');

  return $availability;
}
like image 114
Terry Avatar answered Oct 13 '25 21:10

Terry


This works better in WC > 3.0. Just change get_total_stock to get_stock_quantity

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
  global $product;
  $stock = $product->get_stock_quantity();

  if ( $_product->is_in_stock() ) $availability['availability'] = __($stock . '', 'woocommerce');
  if ( !$_product->is_in_stock() ) $availability['availability'] = __('brak', 'woocommerce');

  return $availability;
}
like image 25
Michal Avatar answered Oct 13 '25 21:10

Michal