WooCommerce’s product edit screen is extensible through hooks. Adding a custom field — a material, a licence key field, a delivery note — takes two steps: render the field in the admin panel, and save its value when the product is updated.
Problem: How do you add a custom input field to a WooCommerce product's admin edit screen and save its value to the database?
Solution: Use the woocommerce_product_options_general_product_data action to output the field, and woocommerce_process_product_meta to sanitise and save the value with update_post_meta().
The example below adds a text field to the General product data tab:
// 1. Render the field
add_action( 'woocommerce_product_options_general_product_data', 'add_product_material_field' );
function add_product_material_field() {
woocommerce_wp_text_input( [
'id' => '_product_material',
'label' => __( 'Material', 'textdomain' ),
'placeholder' => __( 'e.g. Cotton, Steel', 'textdomain' ),
'desc_tip' => true,
'description' => __( 'The primary material this product is made from.', 'textdomain' ),
] );
}
// 2. Save the field
add_action( 'woocommerce_process_product_meta', 'save_product_material_field' );
function save_product_material_field( $post_id ) {
$value = isset( $_POST['_product_material'] )
? sanitize_text_field( $_POST['_product_material'] )
: '';
update_post_meta( $post_id, '_product_material', $value );
}
// 3. Display it on the front end (e.g. on the single product page)
add_action( 'woocommerce_single_product_summary', 'display_product_material', 25 );
function display_product_material() {
global $product;
$material = get_post_meta( $product->get_id(), '_product_material', true );
if ( $material ) {
echo '<p class="product-material">'
. esc_html__( 'Material:', 'textdomain' ) . ' '
. esc_html( $material )
. '</p>';
}
}
NOTE: WooCommerce provides helper functions for all common field types: woocommerce_wp_text_input(), woocommerce_wp_textarea_input(), woocommerce_wp_checkbox(), woocommerce_wp_select(). Using them keeps the admin UI consistent with the rest of WooCommerce.