Woocommerce show different text depending on whether a product is In or Oust of Stock

I was asked by a client to make a modal popup for a product when the thumbnail was clicked on, as part of this popup they wanted the product to show some text letting the user know if it was in stock or not. I have been doing a bit of react.js work recently and boy does react use a lot of ternary operators so I figured one would be helpful in this case. My final code looked like this:

[dt_code]

global $product;
echo $product->is_in_stock() ? ‘Available Now’ : ‘Sorry – out of stock’;

[/dt_code]

Feel free to replace the text with whatever you want to show, the basic formula is:

echo CONDITION ? IF TRUE : ELSE;

 

Confused about ternary operators? This blog shows if, then else statements in React JS and demonstrates how ternary operators can be used to simplify things:

http://devnacho.com/2016/02/15/different-ways-to-add-if-else-statements-in-JSX/

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top