8th Aug '22
/
0 comments

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 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Stock Quantity Block for WP Grid Builder

Stock Quantity Block for WP Grid Builder

In the unofficial WP Grid Builder Facebook group a user asked: Is it possible to show stock quantity on the card? I can't find it…
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
WooCommerce Products Live Search in Bricks

WooCommerce Products Live Search in Bricks

This Pro tutorial provides the steps to set up a live search for WooCommerce products in Bricks. Note: Bricks' live search works with any type…
Categories:
Pro
How to Add Support for Variable Products in Bricks WooCommerce Filters

How to Add Support for Variable Products in Bricks WooCommerce Filters

Filtering for In Stock or Out of Stock WooCommerce products using Bricks' filters currently only works at the product level and not variation level. This…
Categories:
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
How to apply a custom query var to multiple query loops in Bricks

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we'll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function Introduction…
Categories:
[WooCommerce] Product Category Image Background in Bricks

[WooCommerce] Product Category Image Background in Bricks

How to set up the product category thumbnail image as the background of a Section.
Categories: