24th Jul '24
/
0 comments

[WooCommerce] Product Category Image Background in Bricks

[WooCommerce] Product Category Image Background in Bricks

Looking to set up the product category thumbnail image as the background of a Section in Bricks?

Here’s how.

Step 1

Edit your product categories in the WP admin and set images for each.

Step 2

Register a custom function (to be used on WooCommerce term archives) that returns the URL of the product category image.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php 

function bl_get_wc_category_image_url(): string {
    // Check if WooCommerce is active
    if ( ! function_exists( 'is_woocommerce' ) ) {
        return false;
    }

    // Get the category thumbnail ID
    $thumbnail_id = get_term_meta( get_queried_object_id(), 'thumbnail_id', true );

    // If there's no thumbnail ID, return false
    if ( ! $thumbnail_id ) {
        return false;
    }

    // Get the image URL
    $image_url = wp_get_attachment_url( $thumbnail_id );

    // Return the image URL
    return $image_url;
}

Step 3

Whitelist the bl_get_wc_category_image_url function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_wc_category_image_url'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_wc_category_image_url. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 4

Create a Bricks template for product category archives and edit it with Bricks.

Add a Section and inside its Container, a h1 Heading for showing the product category name.

{term_name}

Select the Section → STYLE → BACKGROUND.

Paste this dynamic data tag:

{echo:bl_get_wc_category_image_url}

You could optionally set a gradient color as the overlay.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

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:
Fix for “Your theme (Bricks Child Theme) contains outdated copies of some WooCommerce template files.”

Fix for “Your theme (Bricks Child Theme) contains outdated copies of some WooCommerce template files.”

Updated on 18 Feb 2023 Users of current (at the time of writing this) Bricks stable version of 1.6.2 and WooCommerce 7.4.0 will likely see…
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
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
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: