WooCommerce is a popular platform for eCommerce in WordPress which provides numerous features and functions, but you still may need some things further that it does not provide, such as custom fields for extra information about products. That’s when custom fields created with Meta Box will come in handy. But how can we add those fields to the products which are in an online store built with WooCommerce?
These are two custom fields that I created with Meta Box and added to a product in WooCommerce.
Video Version
Before Getting Started
In this practice, we absolutely need WooCommerce to create and customize the online store.
For custom fields, we need the Meta Box core plugin to have a framework to create them. You can download it directly from wordpress.org.
We also need some Meta Boxes extensions to have some advanced features:
- MB Views to display value from custom fields on the product pages easily;
- Meta Box Builder to has a UI in the backend to create custom fields.
You can install them individually or use Meta Box AIO.
Moreover, you may need some other extensions to have your own field group as you wish in some real cases. But in this case, I keep the field simplest for illustration purposes only. So just go with these two extensions only.
Create Custom Fields
Go to Meta Box > Custom Fields > Add New to create some custom fields.
I will create two fields, as I said, one is cloneable and one is normal. It is just to help you see the difference clearer when displaying them on the product page.
After having all the fields, move to the Settings tab, choose Location as Post type, and select Product to apply these fields to it. Pay attention that this post type is auto-generated by WooCommerce. It is not the post type created with Meta Box.
Now, when editing a product in WooCommerce, you will see the created fields.
Just input some data.
Add Action to Display Fields’ Values
The fields are in the backend so far. If you want to display data saved in the fields on the product page, you should use a hook provided by WooCommerce: 'woocommerce_product_meta_end'.
Normally, you should add that hook to the theme’s file. If you have MB Views from Meta Box, you can add the hook without touching any theme’s files.
Go to Meta Box and create a new view.
Get and Display Data from Fields
In the Template tab, click the Insert field button and look for the field’s name from the list on the right sidebar to get and display information on the product page. Just click on the name of the fields.
The code will be added to the template automatically. You can see the difference in code between the normal field and the cloneable one.
The first line is for the normal field, which is not cloneable. And the three next lines if for the Color field. Since it is cloneable, there will be a loop. This loop also is auto-generated when you insert the Color field.
We will use CSS to improve the displaying of the data from the field. But, before that, you should separate code lines in Div tags and name some classes.
Next, go to the CSS tab of the view, add some code to style the template. There’s also another tab for JavaScript in the event that you need it for some advanced display.
Set the Action
Now, scroll down to the Settings section of the view.
As I said, we will use a hook provided by WooCommerce. So, we will add this template as an action to that hook.
Set the Type of the view as Action. In the Action name box, as 'woocommerce_product_meta_end' which is the name of the hook. You can read more about this hook in WooCommerce documentation for more details.
Now, go back to the page on the frontend, you’ll see the data saved in the fields created with Meta Box displayed on the page.
That’s done!
Last Words
We have finished creating some typical custom fields to add more information to a product created with WooCommerce.
Normally, you should add code to the theme's file following this instruction to add that information to a product and display values saved in custom fields on the WooCommerce’s product page. But, you can see that MB Views from Meta Box can help you shorten and simplify the code much.
Let’s try and share the result with us in the comments.
If you want to suggest any tutorials, feel free to leave a comment and keep track of our blog. Thanks for reading!
- How to Add Custom Fields for WooCommerce - P2 - Using MB Views
Other case studies you might be interested in
- Create A Dynamic Landing Page in WordPress Using Custom Field
- Create a Filter to Find Hotels by Location
- Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
- Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
- Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
- Create Dynamic Favicon in WordPress using Meta Box plugin
- Create Posts Series in WordPress Using Meta Box
- Display a User List On the Frontend with Meta Box
- Display The Latest Products Section - P2 - Using Meta Box and Elementor
- Display The Latest Products Section - P3 - Using Meta Box And Oxygen
- How to Add Custom Fields for WooCommerce - P2 - Using MB Views
- How to Add Custom Fields to Display Banners using Meta Box Plugin
- How to Add Guest Authors and Guest Posts - Using Meta Box
- How to Add Related Posts - Using Custom Fields
- How to Build a Hotel Booking Website Using Meta Box - P1
- How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
- How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
- How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
- How to Create a Classified Ads Website using Meta Box
- How to create a FAQs page - P5 - Using Meta Box and Breakdance
- How to Create a Product Page - P2 - Using Meta Box and Oxygen
- How to Create a Product Page - P3 - Using Meta Box and Bricks
- How to Create a Product Page - P4 - Using Meta Box and Elementor
- How to Create a Product Page - P5 - Using Meta Box and Gutenberg
- How to Create a Product Page - P6 -Using Meta Box and Breakdance
- How to Create a Product Page - P7 - Using Meta Box + Kadence
- How to Create a Product Page - P8 - Using Meta Box and Brizy
- How to Create a Product Page using Meta Box Plugin
- How to Create a Recipe - P2 - Using Meta Box and Oxygen
- How to Create a Recipe - P3 - Using Meta Box and Elementor
- How to Create a Recipe - P4 - Using Meta Box and Bricks
- How to Create a Recipe - P5 - Using Meta Box and Zion
- How to Create a Recipe - P6 - Using Meta Box and Brizy
- How to Create a Recipe - P7 - Using Meta Box and Breakdance
- How to Create a Recipe - P8 - Using Meta Box and Kadence
- How to Create a Recipe with Meta Box Plugin
- How to Create a Simple Listing - P2 - Using Meta Box and Bricks
- How to Create a Team Members Page - P1- Using Meta Box and Elementor
- How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
- How to Create a Team Members Page - P3 - Using Meta Box and Bricks
- How to Create a Team Members Page - P4 - Just Meta Box
- How to Create a Team Members Page - P6 - using Meta Box and Breakdance
- How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
- How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
- How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
- How to Create a Video Gallery Page - P5 - Using MB Views
- How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
- How to Create a Video Gallery Page Using Meta Box + Oxygen
- How to Create ACF Flexible Content Field with Meta Box
- How to Create an Auto-Updated Cheat Sheet in WordPress
- How to Create an FAQs Page - P1 - Using Meta Box and Elementor
- How to create an FAQs page - P2 - Using Meta Box and Oxygen
- How to create an FAQs page - P4 - Using Meta Box and Bricks
- How to Create an FAQs Page - P6 - Using MB Views
- How to Create an FAQs Page -P3- Using Meta Box
- How to Create Buttons with Dynamic Link using Custom Fields
- How to Create Category Thumbnails & Featured Images Using Custom Fields
- How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
- How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
- How to Create Download Buttons Using Custom Fields with Meta Box Plugin
- How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
- How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
- How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
- How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
- How to Create Notification Using Custom HTML Field
- How to Create Online Admission Form for School or University
- How to Create Online Reservation Form for Restaurants using Meta Box
- How to Create Relationships - P1 - Using Meta Box and Oxygen
- How to Create Relationships - P2 - Using Meta Box and Bricks
- How to Create Relationships - P3 - Using MB Views
- How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
- How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
- How to Display Images from Cloneable Fields - P1 - with Gutenberg
- How to Display Images from Cloneable Fields - P2 - with Oxygen
- How to Display Images from Cloneable Fields - P3 - with Elementor
- How to Display Images from Cloneable Fields - P4 - with Bricks
- How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
- How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
- 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
- How to Display the Dynamic Banners - P2 - Using Meta Box and Bricks
- How to Display The Latest Products - P5 - Using Meta Box and Bricks
- How to Display the Latest Products - P6 - using Meta Box and Breakdance
- How to Display the Latest Products - P7 - Using Meta Box + Kadence
- How to Display the Latest Products Section - P4 - Using Meta Box + Zion
- How to Display the Most Viewed Posts - P1 - using MB Views
- How to Display the Most Viewed Posts - P2 - using Meta Box and Oxygen
- How to Display the Most Viewed Posts - P3 - Using Meta Box and Bricks
- How to Filter Posts by Custom Fields - P2 - using Meta Box and FacetWP
- How to Manually Reorder Posts with Meta Box
- How to Show Featured Restaurants on Homepage - P1 - Meta Box + Elementor + WP Grid Builder
- How to Show Posts With a Specific Criteria - P3 - Using MB Views
- How to Show Posts with Specific Criteria - P1 - Using Meta Box and Bricks
- How to Show Posts with Specific Criteria - P2 - Using Meta Box and Oxygen
- How to Show Posts with Specific Criteria - P4 - Using Meta Box + Breakdance
- How to Show Posts with Specific Criteria - P5 - Using Meta Box and Elementor
- How to Show Posts with Specific Criteria - P6 - Using Meta Box and Zion
- How to Show the Featured Restaurants - P3 - using Meta Box and Oxygen