ZenCart show image when out-of-stock PHP code

The goal

To show an image notifying customer that the item is out of stock, but will ship within a certain amount of days (depicted in the image). If you would still like to have the customer be allowed to add to cart, you must set this in the admin panel (Configuration>Stock>Allow Checkout).

The code

I noticed there was quite a few people asking how to do this on the zen cart forums, but no straight answer. The php code is minimal and simple to extremely basic. Hopefully you’ll find this useful and take the time to learn more advanced php coding that will allow you to take full control of your ecommerce website!

Open /includes/YOUR TEMPLATE/templates/tpl_product_info_display.php

Scroll down to where you want to show the image (Perhaps right after Product details list)

insert this php code:
if ($products_quantity == 0) {
echo("<div class=\"productinfostockimage\"></div>");

To briefly explain, this checks the products quantity variable to see if it exactly matches “0” and if it does it places the html code to place a div that we’ll setup to show an image.

Its best not to include the actual image code here to keep the php code clean and to make any changes down the road easier.

Open /includes/YOUR TEMPLATE/css/stylesheet.css

Here we need to create a new css class to display the image for when an item shows as out of stock.

Create a Class that suits your needs

.productinfostockimage {
background-image:url(../images/zerostockimage.png);  //path to image

Save and upload. And you should have a functioning out of stock image that only displays when your items are 0 stock!

Leave a Comment