How to add a “Sold Out” badge for out of stock products in Bricks

How to add a “Sold Out” badge for out of stock products in Bricks

Looking to add a “Sold Out” badge for products in the Shop page for WooCommerce products that are out of stock when using Bricks’ Products element?

One way in which this can be done is by defining a custom function that checks the product object’s is_in_stock method and returns the HTML followed by using this function inside the Products element as a field.

Add this in the child theme’s functions.php or a code snippets plugin:

// Function that returns the HTML for Out of Stock badge when the product is out of stock.
// Can be used in Products element as a dynamic data field: 
function bl_sold_out_badge() {
	// ensure that the global $product variable is in scope
	global $product;

	if ( ! $product->is_in_stock() ) {
		return '<span class="sold-out-badge">Sold Out</span>';
	}
}

Edit your Shop page with Bricks and add this CSS at Settings → PAGE SETTINGS → CUSTOM CODE → Custom CSS:

.sold-out-badge {
  background: #654ea3;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 5px 10px;
  position: absolute;
  right: 10px;
  top: 10px;
}

If you haven’t already, add a Section having the Products element.

Add a new Field (position does not matter – can be at the beginning/end/middle) having:

{echo:bl_sold_out_badge}

Forum post.

Get access to all 525 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Dynamic Data :value filter in Bricks

Dynamic Data :value filter in Bricks

Bricks 1.5.7 introduced a new :value dynamic data filter. This tutorial provides a practical example of how this filter can be used. Meta Box Checkbox…
Categories:
Pro
Top-Level Product Categories in Bricks

Top-Level Product Categories in Bricks

How to show top-level WooCommerce product categories in Bricks using a query loop.
Categories:
Pro
[WooCommerce] Sale price dates in Bricks

[WooCommerce] Sale price dates in Bricks

Outputting start and end sale price dates (if set) for WooCommerce products.
Categories:
How To Remove the Featured Image from WooCommerce Product Gallery

How To Remove the Featured Image from WooCommerce Product Gallery

Product Gallery on single product pages in WordPress include the product's featured image by default. If you are looking to exclude it so only the…
Categories:
Pro
How to Modify Products Element Query in Bricks to Output Child Products

How to Modify Products Element Query in Bricks to Output Child Products

This Pro tutorial provides the steps to limit WooCommerce products output by Bricks' Products element on single product pages to only the children of the…
Condition based on WooCommerce Product Attributes in Bricks

Condition based on WooCommerce Product Attributes in Bricks

How to display elements only when a specific WooCommerce product attribute is selected.
Categories:
Pro
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…