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 610 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:
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
Categories:
Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories:
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
[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:
Pro
ACF Group Sub Field Values from Matching Post Term

ACF Group Sub Field Values from Matching Post Term

In Bricks Reddit, a user asks: Let's break this down and set up the scenario practically. First, there's a Language taxonomy for posts (could be…
Categories:
How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories: