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?!