Display Remaining Stock Quantity on Products
Replaces the generic 'In stock' availability text on WooCommerce product pages with the actual remaining stock count when stock management is enabled for the product.
PHPby SnipCraft
php
<?php
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
add_filter( 'woocommerce_get_availability_text', 'scseed_display_stock_qty', 10, 2 );
function scseed_display_stock_qty( $text, $product ) {
if ( ! $product->managing_stock() || ! $product->is_in_stock() ) {
return $text;
}
$qty = $product->get_stock_quantity();
if ( null === $qty ) {
return $text;
}
return sprintf(
/* translators: %d: number of items remaining */
_n( 'Only %d left in stock — order soon.', '%d in stock.', $qty, 'woocommerce' ),
$qty
);
}#display#inventory#product#stock#woocommerce