Display Out of Stock Badge on Shop Archive Pages

Beginner
✓ Tested
📅 August 23, 2025
⬇️ 0 Downloads
📋 1 Copies

This code snippet adds a visual “Out of Stock” badge/ribbon to products in WooCommerce shop pages, category pages, and anywhere products are displayed in a loop. The badge appears as an overlay on the product thumbnail, making it immediately clear to customers which products are unavailable.

What does this code do?

When a product is out of stock, this code automatically displays a styled ribbon badge on the product image in shop/archive pages. This provides the same visual clarity that customers see on single product pages, but now visible throughout your catalog.

Key Features:

  • Automatic Detection: Automatically checks product stock status and displays badge accordingly
  • Multiple Style Options: Includes 3 different badge styles (ribbon, corner triangle, centered overlay)
  • Image Dimming: Dims out-of-stock product images to further emphasize unavailability
  • Responsive Design: Adjusts badge size for mobile devices
  • Performance Optimized: CSS only loads on shop/archive pages
  • Translation Ready: Uses WooCommerce translation functions

Visual Styles Included:

  1. Ribbon Style (Default): A diagonal red ribbon in the top-right corner
  2. Corner Triangle: A triangular badge in the top-left corner
  3. Centered Overlay: A centered badge with border over the product image

Customization Options:

The snippet includes commented alternative styles that you can easily activate by uncommenting the desired CSS block and commenting out the default style.

Where the badge appears:

  • Shop page
  • Product category pages
  • Product tag pages
  • Search results
  • Related products (optional)
  • Cross-sells and up-sells

⚠️ Important Warnings

⚠️ Always test on a staging site first before applying to production
⚠️ This code requires WooCommerce to be installed and activated
⚠️ Some themes may have conflicting styles - you might need to adjust z-index values
⚠️ If your theme uses custom product loop templates, you may need to adjust the hook priority
⚠️ The :has() CSS selector is not supported in older browsers (pre-2022) - the badge will still show but image dimming won't work
⚠️ Custom shop layouts or page builders might require additional CSS adjustments
⚠️ Make sure your theme properly uses WooCommerce hooks for this to work correctly
💻 PHP Code
/**
 * Display "Out of Stock" badge on WooCommerce shop/archive pages
 * Adds a ribbon/badge overlay on product thumbnails when products are out of stock
 * Prefix: tpsc_ (TP Snippet Collection)
 */

// Add out of stock badge to product loop items
add_action('woocommerce_before_shop_loop_item_title', 'tpsc_display_out_of_stock_badge', 10);

function tpsc_display_out_of_stock_badge() {
    global $product;
    
    if (!$product->is_in_stock()) {
        echo '<div class="tpsc-out-of-stock-badge">' . esc_html__('Out of Stock', 'woocommerce') . '</div>';
    }
}

// Add CSS for the out of stock badge
add_action('wp_head', 'tpsc_out_of_stock_badge_styles');

function tpsc_out_of_stock_badge_styles() {
    if (!is_shop() && !is_product_category() && !is_product_tag() && !is_product_taxonomy()) {
        return;
    }
    ?>
    <style>
        /* Container positioning for the badge */
        .woocommerce ul.products li.product {
            position: relative;
            overflow: hidden;
        }

		.woocommerce li.product {
            position: relative;
            overflow: hidden;
        }
        
        /* Out of Stock Badge - Ribbon Style */
        .tpsc-out-of-stock-badge {
            position: absolute;
            top: 30px;
    		right: -60px;
            background: linear-gradient(135deg, #ff4444 0%, #cc0000 100%);
            color: #ffffff;
            padding: 8px 40px;
            font-size: 12px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            transform: rotate(45deg);
            box-shadow: 0 3px 10px rgba(0,0,0,0.3);
            z-index: 10;
            text-align: center;
            min-width: 150px;
        }
        
        /* Alternative Style 1: Corner Triangle (uncomment to use) */
        /*
        .tpsc-out-of-stock-badge {
            position: absolute;
            top: 0;
            left: 0;
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 80px 80px 0 0;
            border-color: #ff4444 transparent transparent transparent;
            z-index: 10;
        }
        
        .tpsc-out-of-stock-badge::after {
            content: "Out of Stock";
            position: absolute;
            top: -60px;
            left: 5px;
            color: white;
            font-size: 11px;
            font-weight: 700;
            text-transform: uppercase;
            transform: rotate(-45deg);
            width: 80px;
        }
        */
        
        /* Alternative Style 2: Centered Overlay (uncomment to use) */
        /*
        .tpsc-out-of-stock-badge {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.85);
            color: #ffffff;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 14px;
            font-weight: 700;
            text-transform: uppercase;
            z-index: 10;
            border: 2px solid #ff4444;
        }
        */
        
        /* Dim the product image when out of stock */
        .woocommerce ul.products li.product:has(.tpsc-out-of-stock-badge) img {
            opacity: 0.5;
            transition: opacity 0.3s ease;
        }
        
        /* Restore opacity on hover */
        .woocommerce ul.products li.product:hover img {
            opacity: 0.8;
        }
        
        /* Mobile responsive adjustments */
        @media (max-width: 768px) {
            .tpsc-out-of-stock-badge {
                font-size: 10px;
                padding: 6px 30px;
                top: 15px;
                right: -25px;
            }
        }
        
        /* Support for different grid layouts */
        .woocommerce.columns-2 ul.products li.product .tpsc-out-of-stock-badge,
        .woocommerce.columns-3 ul.products li.product .tpsc-out-of-stock-badge,
        .woocommerce.columns-4 ul.products li.product .tpsc-out-of-stock-badge,
        .woocommerce.columns-5 ul.products li.product .tpsc-out-of-stock-badge,
        .woocommerce.columns-6 ul.products li.product .tpsc-out-of-stock-badge {
            /* Ensure badge works with all column layouts */
            position: absolute;
        }
        
        /* Disable add to cart button appearance for out of stock */
        .woocommerce ul.products li.product:has(.tpsc-out-of-stock-badge) .button {
            opacity: 0.5;
            cursor: not-allowed;
        }
    </style>
    <?php
}

// Optional: Customize the badge text via filter
add_filter('tpsc_out_of_stock_text', 'tpsc_customize_out_of_stock_text');

function tpsc_customize_out_of_stock_text($text) {
    // You can change the text here or use translations
    return __('Out of Stock', 'woocommerce');
}

// Optional: Add badge to related products and cross-sells
add_action('init', 'tpsc_extend_out_of_stock_badge_display');

function tpsc_extend_out_of_stock_badge_display() {
    // Add to related products
    if (apply_filters('tpsc_show_badge_on_related', true)) {
        add_action('woocommerce_before_single_product_summary', function() {
            if (is_product()) {
                add_action('woocommerce_before_shop_loop_item_title', 'tpsc_display_out_of_stock_badge', 10);
            }
        }, 5);
    }
}

📝 Installation Instructions

INSTALLATION:
1. Copy the entire code block
2. Add to your theme's functions.php file or use a code snippets plugin
3. Save and refresh your shop page to see the badges

TO CHANGE BADGE STYLE:
1. In the code, find the CSS section with "Alternative Style" comments
2. Comment out the default style (add /* before and */ after)
3. Uncomment your preferred alternative style (remove /* and */)
4. Save and refresh

TO CUSTOMIZE THE TEXT:
Find this line in the code:
return __('Out of Stock', 'woocommerce');
Change 'Out of Stock' to your preferred text

TO CHANGE COLORS:
Look for these values in the CSS:
- Background: #ff4444 and #cc0000 (red gradient)
- Text color: #ffffff (white)
Modify these hex values to match your theme

TO DISABLE ON SPECIFIC PAGES:
Add page conditions to the tpsc_out_of_stock_badge_styles function

TESTING:
1. Set a product to "Out of stock" in WooCommerce
2. Visit your shop page
3. Verify the badge appears on out-of-stock products

📸 Screenshots