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.

Read More
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?

Related posts

Leave a Reply

2 comments

  1. 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;
    }
    
  2. 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;
    }