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:
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:
- MB Custom Post Types & Custom Taxonomies: to create custom post type;
- Meta Box Builder: to have a UI in the backend to create custom fields easily;
- Meta Box Group: to organize custom fields into repeatable and collapsible groups.
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.
After publishing, you’ll see a new Online Shops menu in the admin dashboard.
Let’s create custom fields to save products’ information.
Create Custom Fields
Go to Meta Box > Custom Fields to create a new field group.
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.
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.
Set it to be cloneable by this tick.
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.
Now, when adding a new post in the post type, you’ll see all the created fields. Just fill in some information.
Display the Product Variations on the Product Page
Create a New Template
Go to Bricks > Templates.
Since the product page is a single page, select the template type as Single, then just edit it with Bricks.
Remember to set conditions for the template to apply it to the single page of the products post type.
And then, you can 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.
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.
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.
Then, add a Carousel element inside the Div 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.
Now, you will see all the images display immediately.
Add 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.
For the section to display the color of the product, add another Div element.
Then, add a Basic Text and change 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.
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.
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.
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.
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.
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.
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.
To show the unit of price, you can also add a currency character like this:
For original price, size and status, do likewise.
And then, add a Button element to have the button to order or buy the product.
Let’s see how it displays 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.
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.
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.
<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.
Style the Page
Still in the template editor with Bricks, style any element as you want.
For styling the color, I will add another attribute for the color name like this.
If you want to have further styling, go to the Custom Code section again and add some CSS code.
Now, the page has turned to 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!
- How to Display Product Variations - P1 - Using Meta Box and Gutenberg
- How to Display Product Variations - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P3 - Using Meta Box and Bricks
Other series you might be interested in
- Author Bio
- Better 404 Page
- Blogs for Developers
- Building a Simple Listing Website with Filters
- Building an Event Website
- Building Forms with MB Frontend Submission
- Coding
- Create a Chronological Timeline
- Custom Fields Fundamentals
- Design Patterns
- Displaying Posts with Filters
- Download and Preview Buttons
- Dynamic Banners
- FAQs Page
- Featured Products
- Full Site Editing
- Google Fonts
- Gutenberg
- Hotel Booking
- Latest Products
- Logo Carousel
- MB Views Applications
- Meta Box Builder Applications
- Meta Box Group Applications
- Most Viewed Posts
- Opening Hours
- OTA Website
- Product Page
- Product Variations
- Querying and Showing Posts by Custom Fields
- Recipe
- Restaurant Menus
- SEO Analysis
- Simple LMS
- Speed Up Website
- Taxonomy Thumbnails
- Team Members
- User Profile
- Video Gallery
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?
A lot less bloat if you don't need any of the ecommerce functionality
I think they tried to show a product page that was as realistic as possible.
Hi,
Great tutorial, but how can I use image as a color swatch and how to assign image to each color?
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.