This guide shows you how you can create a custom Text Area field type in the product editor of WooCommerce products. A text area field is particularly useful when you need to convey long pieces of information to customers about a specific product. The purchase note is an example product editor field which uses a text area field type. This guide also shows you how to output the value saved to this new meta box.
/**
* Snippet Name: WooCommerce Custom Product Text Area Metabox
* Snippet Author: ecommercehints.com
*/
// Create the custom product metabox
add_action ('woocommerce_product_options_advanced', 'ecommercehints_custom_product_textarea_metabox');
function ecommercehints_custom_product_textarea_metabox() {
echo '';
woocommerce_wp_textarea_input (array ( // A textarea type field
'id' => 'custom_product_textarea_metabox',
'value' => get_post_meta (get_the_ID(), 'custom_product_textarea_metabox', true),
'label' => 'This is the label',
'description' => 'This is the description',
'desc_tip' => true // If true, place description in question mark tooltip.
));
echo '';
}
// 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_textarea_metabox', $_POST['custom_product_textarea_metabox']);
}
How Do I Get The Custom Text Area Field Data?
global $product;
$custom_product_textarea_metabox = $product->get_meta ('custom_product_textarea_metabox');
echo $custom_product_textarea_metabox;