The WPFolks app is out on iPhone. Free, no ads. Download on the App Store
Skip to main content
Login Join
Snippet · PHP

Auto Add “Out of Stock” Badge on WooCommerce Product Images

Shared by Yash Kapoor · May 1, 2026 · @wp_footer

1 copy
61 views
Back to Snippets

Automatically adds a clean “Out of Stock” badge overlay on WooCommerce product images without modifying templates.

Useful for improving UX and helping users quickly identify unavailable products, especially on stores with large catalogs.

 

Lightweight, no plugin required, and works with most themes.

add_action( 'wp_footer', function () {
    if (! class_exists( 'WooCommerce' ) ) return;
    ?>
    <style>
        .out-of-stock-badge {
            position: absolute;
            top: 10px;
            left: 10px;
            background: #e53935;
            color: #fff;
            padding: 5px 10px;
            font-size: 12px;
            font-weight: bold;
            z-index: 9;
            border-radius: 5px;
        }
        .product {
            position: relative;
        }
    </style>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            document.querySelectorAll('.product').forEach(function (product) {
                if (product.querySelector('.stock.out-of-stock')) {
                    if (!product.querySelector('.out-of-stock-badge')) {
                        let badge = document.createElement('div');
                        badge.className = 'out-of-stock-badge';
                        badge.innerText = 'Out of Stock';
                        product.appendChild(badge);
                    }
                }
            });
        });
    </script>
    <?php
} );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly sponsored by