WooCommerce Custom Text Metabox For Products

WooCommerce custom text field shown in the product editor

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

By default, WooCommerce allows editing for a range of fields including the product’s weight, height, it’s menu order, it’s price…the list goes on! However, it can sometimes be necessary to allow further data to be added to products which are not available by default. This guide shows you how you can create a custom product editor text field (or metabox) an how to get the data entered.

				
					/**
 * Snippet Name:     WooCommerce Custom Product Text Metabox
 * Snippet Author:   ecommercehints.com
*/

// Create the custom product metabox
add_action ('woocommerce_product_options_advanced', 'ecommercehints_custom_product_text_metabox');
function ecommercehints_custom_product_text_metabox() {
   echo '<div class="options_group">';
   woocommerce_wp_text_input (array ( // A text box type field
      'id'                => 'custom_product_text_metabox',
      'value'             => get_post_meta (get_the_ID(), 'custom_product_text_metabox', true),
      'label'             => 'This is the label',
      'description'       => 'This is the description',
      'desc_tip'          => true // If true, place description in question mark tooltip.
  ));
   echo '</div>';
}

// Save data entered in the product metabox
add_action ('woocommerce_process_product_meta', 'ecommercehints_save_field_on_update', 10, 2);
function ecommercehints_save_field_on_update ($id, $post) {
      update_post_meta ($id, 'custom_product_text_metabox', $_POST['custom_product_text_metabox']);
}
				
			

How Do I Output The Data?

				
					global $product;
$custom_product_text_metabox = $product->get_meta ('custom_product_text_metabox');
echo $custom_product_text_metabox;
				
			
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

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

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers