Do you want to add a custom meta box to add extra information on the single product page?
Then stick to the end, as this post aims to provide a custom code snippet we made specifically to solve this problem.
Of course, this means that you need to have some coding experience for you to implement this solution.
WooCommerce has been created for all kinds of eCommerce websites. However, it only satisfies the basic needs of an online store.
This means that if your products are specific, you might need to add additional product information to help your customers make an informed decision.
One way of doing this is to create a custom meta box on the Edit Product page.
Then, after the information is saved, it will be displayed on the single product page.
However, if you do a quick search, you will find many plugins to solve this problem.
However, if you have many plugins, they will end up bloating your site. As a result, the loading speed of your site will be affected negatively. This is why we decided to create this tutorial for you.
Add Custom Meta Box in WooCommerce Product
In today’s brief tutorial, we will show you how to add a custom meta box in your WooCommerce store.
Using custom code snippets is the recommended way of making changes in WordPress.
Before you proceed, you should install or create a child theme. This will ensure that your changes are not lost during an update.
Without wasting much time, let us get right into it.
Steps to Add Custom Meta Box in WooCommerce Product
Here are the simple steps you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to add a custom meta box in the WooCommerce product.
- Add the following code to the PHP file:
## ---- 1. Backend ---- ## // Adding a custom Meta container to admin products pages add_action( 'add_meta_boxes', 'create_custom_meta_box' ); if ( ! function_exists( 'create_custom_meta_box' ) ) { function create_custom_meta_box() { add_meta_box( 'custom_product_meta_box', __( 'Additional Product Information <em>(optional)</em>', 'cmb' ), 'add_custom_content_meta_box', 'product', 'normal', 'default' ); } } // Custom metabox content in admin product pages if ( ! function_exists( 'add_custom_content_meta_box' ) ){ function add_custom_content_meta_box( $post ){ $prefix = '_bhww_'; // global $prefix; $ingredients = get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) : ''; $benefits = get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) : ''; $args['textarea_rows'] = 6; echo '<p>'.__( 'Ingredients', 'cmb' ).'</p>'; wp_editor( $ingredients, 'ingredients_wysiwyg', $args ); echo '<p>'.__( 'Benefits', 'cmb' ).'</p>'; wp_editor( $benefits, 'benefits_wysiwyg', $args ); echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">'; } } //Save the data of the Meta field add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 ); if ( ! function_exists( 'save_custom_content_meta_box' ) ) { function save_custom_content_meta_box( $post_id ) { $prefix = '_bhww_'; // global $prefix; // We need to verify this with the proper authorization (security stuff). // Check if our nonce is set. if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) { return $post_id; } $nonce = $_REQUEST[ 'custom_product_field_nonce' ]; //Verify that the nonce is valid. if ( ! wp_verify_nonce( $nonce ) ) { return $post_id; } // If this is an autosave, our form has not been submitted, so we don't want to do anything. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } // Check the user's permissions. if ( 'product' == $_POST[ 'post_type' ] ){ if ( ! current_user_can( 'edit_product', $post_id ) ) return $post_id; } else { if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id; } // Sanitize user input and update the meta field in the database. update_post_meta( $post_id, $prefix.'ingredients_wysiwyg', wp_kses_post($_POST[ 'ingredients_wysiwyg' ]) ); update_post_meta( $post_id, $prefix.'benefits_wysiwyg', wp_kses_post($_POST[ 'benefits_wysiwyg' ]) ); } } ## ---- 2. Front-end ---- ## // Create custom tabs in product single pages add_filter( 'woocommerce_product_tabs', 'custom_product_tabs' ); function custom_product_tabs( $tabs ) { global $post; $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true ); $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true ); if ( ! empty( $product_ingredients ) ) $tabs['ingredients_tab'] = array( 'title' => __( 'Ingredients', 'woocommerce' ), 'priority' => 45, 'callback' => 'ingredients_product_tab_content' ); if ( ! empty( $product_benefits ) ) $tabs['benefits_tab'] = array( 'title' => __( 'Benefits', 'woocommerce' ), 'priority' => 50, 'callback' => 'benefits_product_tab_content' ); return $tabs; } // Add content to custom tab in product single pages (1) function ingredients_product_tab_content() { global $post; $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true ); if ( ! empty( $product_ingredients ) ) { echo '<h2>' . __( 'Product Ingredients', 'woocommerce' ) . '</h2>'; // Updated to apply the_content filter to WYSIWYG content echo apply_filters( 'the_content', $product_ingredients ); } } // Add content to custom tab in product single pages (2) function benefits_product_tab_content() { global $post; $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true ); if ( ! empty( $product_benefits ) ) { echo '<h2>' . __( 'Product Benefits', 'woocommerce' ) . '</h2>'; // Updated to apply the_content filter to WYSIWYG content echo apply_filters( 'the_content', $product_benefits ); } }
Conclusion
In today’s tutorial, you have seen how it is easy to add a custom meta box in WooCommerce.
This will help you customize your website for your specific products and offer more information to your customers.
If you are not familiar with handling code, we recommend using a plugin like Meta Box.
We hope that this post provided a solution for you.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- What is the Best Payment Gateway for WooCommerce
- How to Hide Featured Image on a Single Product Page
- How To Display Product Variations In WooCommerce
- How to Get Current Product WooCommerce
- How to Change Email Template in WooCommerce
- How to Add Custom Background For WooCommerce
- How to Use WooCommerce to Set Up Online Store
- How to Upload Image On Product Page In WooCommerce
- How to Add Star Rating To WooCommerce Product
- WooCommerce Create Custom Single Product Page
- How to Customize WooCommerce Product Pages With Elementor Pro
- How to Add Information to the WooCommerce Shop Page
- How to Add View Cart Button In WooCommerce
- How to Hide Product Gallery In WooCommerce
- How to Add Custom Taxonomy To WooCommerce Products
- How to Add New Column On WooCommerce Orders Page
- How to Move WooCommerce Products To New Site