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

Related Tutorials..

Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.
Pro
Setting a Bricks template for Parent pages

Setting a Bricks template for Parent pages

This Pro tutorial shows how bricks/active_templates filter can be used to programmatically assign a Bricks template to all singular pages (of any post type) that…
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…
Pro
Similarly Priced WooCommerce Products Bricks Query Loop

Similarly Priced WooCommerce Products Bricks Query Loop

How to show products priced within a percentage of the current single product's price.
Categories:
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
On Sale Ribbon for WooCommerce Products Query Loop in Bricks

On Sale Ribbon for WooCommerce Products Query Loop in Bricks

Showing a SALE ribbon for products that are output using Bricks‘ Query Loop.
Categories: