I need an extra class for the stock notice on the WooCommerce product page. At the moment, there are two different classes in-stock
and out-of-stock
. But there is a third option in WooCommerce. If there a only few items in stock, the class would also be in-stock
. It would be nice to have something like few-in-stock
.
I checked the template file stock.php
which generates the output. There is only one line of code:
<p class="stock <?php echo esc_attr( $class ); ?>"><?php echo wp_kses_post( $availability ); ?></p>
And the $class
has only the two options from above. Is there any function I could use to add a third class?
At the moment I count the stock items of a product like this:
$stock_count = $product->get_stock_quantity();
And add my new class like this:
<p class="stock <?php echo esc_attr( $class ); ?> <?php if ( $stock_count <= '2' ) : ?>few-in-stock<?php endif; ?>"><?php echo wp_kses_post( $availability ); ?></p>
But maybe there is a better way to do that?!
Here is the function hooked in woocommerce_get_stock_html
filter hook, that will allow you to add an extra stock notice for low stock (with custom CSS):
add_filter( 'woocommerce_get_stock_html', 'filter_get_stock_html', 10, 2 );
function filter_get_stock_html( $html, $product ) {
// Low stock quantity amount
$low_stock_qty = 3;
$availability = $product->get_availability();
if ( ! empty( $availability['availability'] ) ) {
$class = esc_attr( $availability['class'] );
$avail_text = wp_kses_post( $availability['availability'] );
$stock_qty = $product->get_stock_quantity();
if( $stock_qty <= $low_stock_qty ){
$class .= ' few-in-stock';
$avail_text = __('Few in stock', 'woocommerce');
}
ob_start();
// Make your changes below
?>
<p class="stock <?php echo $class; ?>"><?php echo $avail_text; ?></p>
<?php
$html = ob_get_clean();
}
return $html;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Get optionally the low stock amount from general product inventory settings
If you have set "low stock threshold" in
Settings
>Product
>Inventory
, you can get it and replace the manual inputed value, replacing the following line:// Low stock quantity amount $low_stock_qty = 3;
By this:
// Low stock quantity amount $low_stock_qty = get_option('woocommerce_notify_low_stock_amount');
(Thanks to @Demian)
The answer from @LoicTheAztec is a good start. However, a missing keypoint is that you want to reflect the "Low stock threshold" from the backend.
In his answer you have to manually fill in what the low stock quantity is if( $stock_qty <= 10 ){
. However, woocommerce backend Settings > Product > Inventory does have the amount recorded. So you want ideally to fill in this line with:
if( $stock_qty <= get_option( 'woocommerce_notify_low_stock_amount' ) ){
This way you don't have to dig into your functions file if you want to change the amount.
So the complete code would be:
add_filter( 'woocommerce_get_stock_html', 'filter_get_stock_html', 10, 2 );
function filter_get_stock_html( $html, $product ) {
$availability = $product->get_availability();
if ( ! empty( $availability['availability'] ) ) {
$class = esc_attr( $availability['class'] );
$avail_text = wp_kses_post( $availability['availability'] );
$stock_qty = $product->get_stock_quantity();
if( $stock_qty <= get_option( 'woocommerce_notify_low_stock_amount' ) ){ // reflects backend for Low stock threshold
$class .= ' low-in-stock';
$avail_text = __('Low in stock', 'woocommerce');
}
ob_start();
// HTML reflection:
?><p class="stock <?php echo $class; ?>"><?php echo $avail_text; ?></p><?php
$html = ob_get_clean();
}
return $html;
}
Extra: If you don't want negative stock quantity (i.e. -10) to be involved in the above low-in-stock
class, change the following line from above snippet
if( $stock_qty <= get_option( 'woocommerce_notify_low_stock_amount' ) ){
with this:
if( $stock_qty >= 0 && $stock_qty <= get_option( 'woocommerce_notify_low_stock_amount' ) ){
It sets the low-in-stock
class between 0 and the quantity you have set in the backend woocommerce settings.