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
Out of Stock Ribbon for WooCommerce Products in Bricks

Out of Stock Ribbon for WooCommerce Products in Bricks

Show an “OUT OF STOCK” ribbon for WooCommerce products that are output via the Products element on listing pages.
Categories:
Pro
Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Update on 23 Aug 2023: Added steps for ACF. Bricks builder v1.8.4 introduced an pretty useful filter called bricks/active_templates that flew under the radar. This…
Appending WooCommerce Endpoint Names To My account Page Titles in Bricks

Appending WooCommerce Endpoint Names To My account Page Titles in Bricks

Adding the WooCommerce endpoint names, “Orders”, “Downloads” after the WooCommerce ‘My account’ Page’s title text at the various endpoint URLs.
Categories:
Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Side-by-side WooCommerce Quantity and Add to Cart in Bricks

Looking to position the quantity input field and Add to cart button next to each other instead of one below the other in Bricks? Add…
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
WooCommerce Product Carousel in Bricks

WooCommerce Product Carousel in Bricks

How to set up a carousel that shows WooCommerce products with the product title, price and Add to Cart button for each.
Categories: