'Add a text next to stock quantity if it is less than 10 in Woocommerce single product

I found this posted as a comment on another thread but I don't know what I'm supposed to do with it:

 if($product->get_stock_quantity() <10){echo 'Limited supply left';}

I figured it should have a hook. I tried woocommerce_after_single_variation but it didn't work



Solution 1:[1]

Using woocommerce_get_availability_text filter hook with the following:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {

    if( $product->is_in_stock() && $product->managing_stock() && 
    ! $product-> is_on_backorder( 1 ) && $product->get_stock_quantity() < 10 ) {
        $availability_text .= ' ' . __("(limited supply left)", "woocommerce");
    }
    return $availability_text;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1