With this snippet, I’ll show you how to display a better Woocommerce out of stock message which will hopefully result in more sales and happier customers. Unlike most other turorials which simply modify the message
that appears, this snippet will promote content which is currently available without breaking the look and feel of the site. I’ve been using this method for quite a while with great success and highly recommend it.
Before we get too far into this post I should make a few points. For best results, it’s highly recommended that you segment your content into relevant categories and tags. This default snippet will make use of the related products shortcode, but you can substitute the related with any other Woocommerce shortcode that you want to use. You can also tie this in with our snippet to change the actual out of stock message text for an even better buyer experience
The code snippet for a better Woocommerce out of stock message
/*
** Snippet Out of stock up sell
** source code https://wcsuccessacademy.com/?p=435
** Author John Cook
** Tested with WooCommerce 8.8.2
*/
add_action( 'woocommerce_before_single_product_summary', 'my_related_out_of_stock', 15);
function my_related_out_of_stock () {
global $product;
$availability = $product->get_availability();
/*check if availability in the array = string 'Out of Stock'
**if so display on page.
*/
if ( $availability['availability'] == 'Out of stock') {
echo '<h2 style="text-align: center; text-decoration: underline; font-size: 2em;">This item is currently out of stock</h2>';
echo '<h3 style="text-align: center;">Perhaps one of the following may interest you?</h3>';
echo do_shortcode( '[related_products]' );
}
}
Where does this snippet go?
Place this code snippet at the end of your child themes functions.php file just before the closing ?>(if your theme has it). Alternatively, and recommended, place the code snippet into a functions plugin, such as one on this list. The use of a functions plugin will ensure that changes are kept across themes and updates.
You can further modify the code using different shortcodes and css to get a better look and more relevant results. I do honestly believe that this is a a better Woocommerce out of stock message than the packaged one or any modification of it.
Did it work?
Let me know in the comments if it worked for you. With a bit of styling and some modifications to the code you can extend the use and upsell potential of this code snippet.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch