In e-commerce websites, each product may have multiple variations to separately choose color, size and even the stock status for them. Let’s see how to have product variations using custom fields created by Meta Box and display them on a single page by Bricks.

This is the page that I created as an example:

The example of displaying product variations on the single product page.

Each of the variations here is set as a color of the product. When you choose a color, it will get the corresponding image gallery. When you choose another one, it will get a different gallery. At the same time, other information of that variation will automatically come up.

Creating variations for the products is quite special practice. If you are using some of the other page builders, you may need to use code. However, you can do it totally visually with Bricks.

Let’s see how to do it.

Video Version

Before Getting Started

Each product is a post of a custom post type. The name and description of the product is the title and content of the post. Other information such as product image, size, price and status will be saved in custom fields created by Meta Box. So we need Meta Box core plugin to have a framework to create custom fields and custom post types.

Besides, we also need some of Meta Box extensions to have advanced features:

Finally, having Bricks to build this page visually.

Create a Custom Post Type

Go to Meta Box > Post Types > Add New to create a new post type for the products.

Create a new post type

After publishing, you’ll see a new Online Shops menu in the admin dashboard.

A new menu appears after creating a new post type.

Let’s create custom fields to save products’ information.

Create Custom Fields

Go to Meta Box > Custom Fields to create a new field group.

Create custom fields by Meta Box

Since each variation will have the same types of detailed information, we’ll include them all in a group. This is the group field that I created.

Create variations of product group

Variations Product group with subfields inside

There may be some variations of each product, so this group should be cloneable to have an ‘Add more’ button to easily add more variations like this.

Add more variations

Set it to be cloneable by this tick.

Set the Variations of Product group as cloneable to have more spaces to add variations.

After that, move to the Settings tab > choose Location as Post Type and then select the Online Shop post type to apply these fields to it.

Choose the location as post type of the product.

Now, when adding a new post in the post type, you’ll see all the created fields. Just fill in some information.

The created custom fields are displayed in post editor

Display the Product Variations on the Product Page

Create a New Template

Go to Bricks > Templates.

Go to Bricks > Templates to create new template

Since the product page is a single page, select the template type as Single, then just edit it with Bricks.

Select the template type as Single and edit it with Bricks

Remember to set conditions for the template to apply it to the single page of the products post type.

Set conditions for the template

And then, you can set the preview of the products page in the Populate Content section.

Set the preview of the products page in the populate content section

Display Product’s Information

First, add a Section element to have a container for covering all the product information.

Add a section element to have a container to cover all the product information

Then, I will add some Div elements to set the layout for the page. We’ll use a Div element to contain each section for each one of gallery, color, price, size, and status. Inside this cloak Div element, there will be another Div to create a loop to call all the data of the section.

Add some Div elements to set the layout for the page

Since each variation will have its own image gallery, you should add a Div element and enable the query loop for it to call all the images. Choose the type as MB Group which is the group that I created to save the product details.

Choose the type as MB Group in a Div element setting

Then, add a Carousel element inside the Div to display the images as a slider.

Add a Carousel element to display the images as a slider

Since the images of the product are saved in the custom fields created by Meta Box, go to the Select dynamic data section of the Carousel and look for the fields that we’ve saved the images.

Looking for the fields was created at the the Select dynamic data section

Now, you will see all the images display immediately.

The product images have already displayed

Add a new Div element to contain all the other product information.

Create a new Div element to contain all the other product information

Inside it, add the Post Title and Post Content elements to display the products’ name and description.

Input the post title and post content elements to display the products’ name and description

For the section to display the color of the product, add another Div element.

Add another Div element to display the color of the product

Then, add a Basic Text and change the text to differentiate this section.

Insert a Basic Text and move the text to differentiate this section

I’ll also add some Div elements for this section as well. One is to display the color labels for the variations. So, enable the query loop for it. Then, also choose the name of the group field that we’ve just created.

Enable the query loop and choose the name of the group field

Now, to get the color information, add another Div element, then choose HTML tag as a link. This helps to allow the viewer to click on the color name and the information will be changed to one of the corresponding variations. And then, select the link type as Dynamic Data to have the options to connect this div to a custom field. In my case, it's the Color Name field.

Select the link type as Dynamic Data to have the options to connect the new div to a custom field

You may want to not redirect the viewer to a new page when choosing color. So add an additional hash character. As a result, just information on the current page will be changed when clicking to select the color.

Use the Div element to show information

Next, add a Basic Text element, then use the Select dynamic data button to get the color information of the product from the custom field.

Apply the select dynamic data button to get the color information of the product from the custom field.

For the rest of the information, use the Div element as you want. It’s kind of the logic of getting color. So, just follow what has been done to get the color of the product.

Use the Div element to show information

There will be a Div element to contain the prices. There will be multiple variations and each one will have its own price, which are saved data in the cloneable group, so also enable the query loop for this Div element, and choose the group name that we’ve created.

Enable the query loop and choose the group name that we’ve created for product

After that, add a Basic Text element to display the promotional price. Similarly to get the color information, use the Select dynamic data button and connect this element to the corresponding field.

Using the Select dynamic data button for connecting this element to the corresponding field

To show the unit of price, you can also add a currency character like this:

Add a currency character to show the unit of price of product

For original price, size and status, do likewise.

And then, add a Button element to have the button to order or buy the product.

Including a button element to have the button to order or buy the product

Let’s see how it displays on the frontend.

Product information displayed on the frontend

Set Rules to Display Variations

To trigger when the viewer clicks on the color, then change the information to the corresponding one, we should use some attributes for the added elements and JS code.

Go to the cloak Div element that contains each section for gallery, color name, price, size, and status to add attribute like this.

Using some attributes for the added elements and JS code to change the information to the corresponding one

Since all the sections of the variation will be called at the same time based on these attributes, make sure to use an unifying name for all the adding attributes. Also, set the attributes’ value as the value in the Color Name field by using the dynamic data.

Using the dynamic data to set the attributes’ value as the value in the Color Name field

After that, go to the settings of this template > Page Settings > Custom Code to add some JS to the Body (footer) scripts section to create a function for triggering the moment users click to the color name, then activate and deactivate the information of the corresponding variations.

Insert some JS to the Body (footer) scripts section to create a function for triggering the moment users click to the color name

<script>
    jQuery(document).ready(function(){
        jQuery(".color-name:first,.group-image:first,.price-group:first,.info:first,.status:first").addClass('active');
        jQuery(".color-contain-group .color-group .color-name a").click(function (e) {
        jQuery(".color-contain-group .color-group .color-name").removeClass("active");
        jQuery(this).show();
        jQuery(this).parent().addClass("active");
        jQuery("div[data-id]").removeClass("active");
        jQuery("div[data-id='" + jQuery(this).attr("href").replace("#", "") + "']").addClass("active");
        e.preventDefault();
        });
    });
</script>

In there, jQuery(".color-contain-group .color-group .color-name a").click(function (e) { is to trigger when someone clicks on a product color. Then admit the name of the selected color by setting it active. Compare it with the value of the added attribute from other elements. If they are matched, the information from those elements will be displayed.

Now, you can see the active status changing when choosing any color.

The active status changing when choosing any color

Style the Page

Still in the template editor with Bricks, style any element as you want.

Styling any element as you want with Bricks

For styling the color, I will add another attribute for the color name like this.

Add another attribute for the color name

If you want to have further styling, go to the Custom Code section again and add some CSS code.

Add some CSS code in the Custom Code section to have further styling

Now, the page has turned to a new look.

The product variations turned into a new look.

Last Words

With the help of Meta Box, you can create and show product variations with not only Bricks but other page builders as well. If you want to refer to how to do it using other page builders or tools, please read more here. Or, if you want to suggest any topics for tutorials, feel free to leave a comment and keep track of our blog. Best wishes!

5 thoughts on “How to Display Product Variations - P3 - Using Meta Box and Bricks

  1. Great content as always, however, why should I use a MetaBox Custom Post Type to display products on my WordPress website instead of a prebuilt solution like WooCommerce?

    1. To do it, you can use the Single Image field (instead of the Select field). Then, add a condition for the Image element instead of the Basic Text element (similar to the tutorial). Please try and tell us the result.

Leave a Reply

Your email address will not be published. Required fields are marked *