Pierre3895
Pierre3895

Reputation: 23

Change WooCommerce product availability In stock text by a custom text

I would like to change the text "in stock" behind the number in stock. I tried adding this PHP code in my wordpress php editor but it doesn't work. Do you have any idea why? Thank you !

add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
  
function bbloomer_custom_get_availability_text( $availability, $product ) {
   $stock = $product->get_stock_quantity();
   if ( $product->is_in_stock() && $product->managing_stock() ) $availability = $stock. 'remaining copies' ;
   return $availability;
}

Upvotes: 2

Views: 2173

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Try the following instead:

add_filter( 'woocommerce_get_availability', 'filter_product_availability', 10, 2 );
function filter_product_availability( $availability, $product ) {
    $stock_quantity = $product->get_stock_quantity(); // get stock quantity

    if ( $product->is_in_stock() && $stock_quantity > 0 ) {
        $availability['availability'] = sprintf( __('%d remaining copies'), $stock_quantity );
    }
    return $availability;
}

Or also you can try this:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {
    
    return str_replace( 'in stock', __('remaining copies', 'woocommerce'), $availability_text );
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Upvotes: 1

Collie-IT Anne K. Frey
Collie-IT Anne K. Frey

Reputation: 875

You can use the in build option for this go to WooCommerce->Preferences->Products->Inventar

enter image description here

and choose the first option. Show available Products and save.

Upvotes: 0

Related Questions