14th Sep '23
/
5 comments

Conditional Bricks Templates Based on Product Category Hierarchy Levels

A user wrote:

My products are categorized in hierarchised product categories (nested categories), as you can see in attachment. With different levels...

I'd like to create differten templates for product archives depending on the level of these hierarchies, for example: Level 1 hierarchy --> Template 1, then Level 2 hierarchy --> Template 2... and so on... not a specific product category, but the level of hierarchy is the criteria.

How would you tackle this? I don't see how I could do this with the template conditions... and I need it to be compatible with the very soon feature WPML compatible (being right now developed by Thomas from Bricks), my products / product categories have many languages - I guess this won't be a problem, but I prefer mentionning it if you see it as problematic...

This Pro tutorial provides the steps to programmatically apply Bricks templates to WooCommerce product category archives based on the hierarchy level.

The first thing we need to do when tackling such requirements is define or put in words, what makes up our conditions. In this case, that would be the following.

Level 1: has no parent, should have at least 1 child.

Level 2: should have a parent, parent should not have parent, should have at least 1 child.

Level 3: should have a parent, parent's parent should not have parent.

I wrote the above after creating and looking at three test product categories like this:

Then we create custom PHP functions named say, bl_level1_product_cat(), bl_level2_product_cat() and bl_level3_product_cat() and use these in a function hooked to bricks/active_templates filter.

Step 1

If you have not already, create a template that applies to all product category archives. Ensure that the template condition is set to Archive → Categories & Tags → All terms (Product categories). This template will be like a fallback and gets used/applied for all the product categories that do not meet the criterion for the three levels we defined earlier.

Next, create different templates for Level 1, Level 2 and Level 3. Do not set any template conditions for these.

When done, it should appear something like this:

Note the IDs of Level 1, 2 and 3 templates. We need these in the next step.

Step 2

Add the following in child theme's functions.php or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Conditional Single CPT Templates based on Post Hierarchy in Bricks

Conditional Single CPT Templates based on Post Hierarchy in Bricks

In the past we covered Conditional Bricks Templates Based on Product Category Hierarchy Levels in Bricks. This Pro tutorial provides the steps to programmatically apply…
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
On Sale Products Bricks Query Loop

On Sale Products Bricks Query Loop

Looking to show only the WooCommerce products that are on sale in Bricks? Set up a query loop, enable the PHP query editor and paste:
Categories:
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
Product Attribute based Related Products Bricks Query Loop

Product Attribute based Related Products Bricks Query Loop

How to output products related by value of attribute(s) for the current product on single WooCommerce product pages.
Categories:
Pro
Current Post Has Parent – Conditional Single Template

Current Post Has Parent – Conditional Single Template

How to apply one of two Bricks templates to posts depending on whether that post has a parent or not.