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.
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.
The snippet includes commented alternative styles that you can easily activate by uncommenting the desired CSS block and commenting out the default style.
/**
* 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);
}
}