I'm trying to change the qty of stock message on a single product page if the stock is below 4, otherwise, show the default message.
What I want : When the stock is lower then 4 (for example 3), I want to show "Low stock" instead of "3 in stock".
I think I need to add a filter to modify what is showing there but not sure. I found a code online that seems to be close to what I'm looking for but I don't know what is missing to make it work.
function load_variation_settings_fields( $variation_data, $product, $variation ) {
$qty->get_stock_quantity()
if( $variation-> $qty < 3 )
$variation_data['text_field'] = __('Low stock');
else $variation_data['text_field'] = __($qty . 'in stock');
return $variation_data; }
Can you help me to make the right PHP code to make this function works?
Thank you very much.
Jeff
you can use woocommerce_get_availability
to achieve that as following:
add_filter( 'woocommerce_get_availability', 'change_stock_text', 20, 2 );
function change_stock_text( $availability, $_product ) {
if ( $_product->is_in_stock() ) {
if ( $_product->get_stock_quantity() < 4 ) {
$qty = $_product->get_stock_quantity();
$availability['availability'] = __( "{$qty} Low Stock", 'woocommerce' );
}
}
return $availability;
}
Output: