skip to Main Content

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;
}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search