The client has a sale category which has great deals on items with limited stock. In order to give people additional incentive to buy right away, he wants to display a message when inventory falls below a certain quantity.
In order to do this, we are going to write a function named show_quantity_in_stock
using the add_action()
function, which will be hooked into the woocommerce_single_product_summary
action with a priority of 10.
add_action( 'woocommerce_single_product_summary', 'show_quantity_in_stock', 10 );
function show_quantity_in_stock() {
// do something if certain conditionals are met.
}
The woocommerce_single_product_summary
action is a hook provided by WooCommerce that is triggered within the single product page template (single-product.php
) and allows us to insert custom content, functions, or modifications into the product summary area of the single product page.
Next, we need to gather some information about the product. First, we access the global $product
object to retrieve the stock quantity using $product->get_stock_quantity()
and assign it to the variable $stock_quantity
.
Then, we check to see if it matches our first condition – if the product has the “sale” category assigned. We do this using the has_term()
function, and hard-code the category slug. (We could also set up a variable and assign the value via a UI, but that’s outside of the scope of this). If it meets this condition we move on to the second condition.
add_action( 'woocommerce_single_product_summary', 'show_quantity_in_stock', 10 );
function show_quantity_in_stock() {
global $product;
$stock_quantity = $product->get_stock_quantity();
// Check if the product is in the "sale" category.
if ( has_term( 'sale', 'product_cat', $product->get_id() ) ) {
// Check with second conditional, and then do something.
}
}
In the second conditional block, we further check if the $stock_quantity
is not empty, greater than 0, and less than 26. This condition ensures that we only display the stock quantity for products within the desired quantity range.
if ( '' !== $stock_quantity && $stock_quantity > 0 && $stock_quantity < 26 ) {
// Do something.
}
That conditional will be nestled inside the first conditional. And then, if the product passes all tests – if it is in the “sale” category, and if it has an inventory value, and if that value is over 0 and under 26, it will output an alert.
The entire function, with comments, is below. Place it on functions.php or on any other file that contains functions. (I have all my woocommerce functions on a woocommerce.php file that is called on functions.php if the class ‘Woocommerce’ exists, which exists globally if the WooCommerce plugin is installed and activated. This would allow me to use this same theme for a site that is not a WooCommerce site, in which case a bunch of unneeded WooCommerce functions would not be called).
add_action( 'woocommerce_single_product_summary', 'show_quantity_in_stock', 10 );
/**
* Show quantity in stock on product page if in Sale category.
*
* @return void
*/
function show_quantity_in_stock() {
global $product;
$stock_quantity = $product->get_stock_quantity();
// Check if the product is in the "sale" category.
if ( has_term( 'sale', 'product_cat', $product->get_id() ) ) {
$stock_quantity = $product->get_stock_quantity();
if ( '' !== $stock_quantity && $stock_quantity > 0 && $stock_quantity < 26 ) {
echo '<div class="product__info-stock">';
echo '<p>Only <span class="bold-stock"> ' . esc_html( $stock_quantity ) . ' left in stock!!</span></p>';
echo '</div>';
}
}
}
Because we’re using the woocommerce_single_product_summary
action hook, this will automatically run on a single product page. In this case we also want to call this function on the product category archive page, which uses the loop template content-product.php, which in our theme has been heavily modified. In order to run the function there, we just call it where we want it to run:
<div class="product__low-stock">
<?php
show_quantity_in_stock();
?>
</div>
That’s it!