Custom WooCommerce product fields with validation
A store needs a value per product that WooCommerce does not have, for example a lead time in days. The field belongs in the product data box, next to price and SKU, and should be saved through the product object rather than with raw update_post_meta(), so it works with the WooCommerce data layer, caches and the REST API.
Problem: Each product needs a “Lead time (days)” number between 0 and 60, editable on the General tab, shown on the product page and available to code.
Solution: Print the field with woocommerce_wp_text_input() on woocommerce_product_options_general_product_data, validate and store it on woocommerce_admin_process_product_object with $product->update_meta_data(), show an admin notice for invalid input, and read it with $product->get_meta().
The field
add_action( 'woocommerce_product_options_general_product_data', function () {
echo '<div class="options_group">';
woocommerce_wp_text_input( [
'id' => '_ha_lead_time',
'label' => __( 'Lead time (days)', 'textdomain' ),
'desc_tip' => true,
'description' => __( 'Days until the product ships. Leave empty if it ships the same day.', 'textdomain' ),
'type' => 'number',
'custom_attributes' => [ 'min' => 0, 'max' => 60, 'step' => 1 ],
] );
echo '</div>';
} );
woocommerce_wp_text_input() reads the current value from the product’s meta by its id and prints the field in the standard WooCommerce markup. The min and max attributes help in the browser, but they are not a check.
Validate and save
add_action( 'woocommerce_admin_process_product_object', function ( WC_Product $product ) {
if ( ! isset( $_POST['_ha_lead_time'] ) ) {
return;
}
$raw = trim( wp_unslash( $_POST['_ha_lead_time'] ) );
if ( '' === $raw ) {
$product->delete_meta_data( '_ha_lead_time' );
return;
}
if ( ! ctype_digit( $raw ) || (int) $raw > 60 ) {
WC_Admin_Meta_Boxes::add_error( __( 'Lead time must be a whole number from 0 to 60. The old value was kept.', 'textdomain' ) );
return;
}
$product->update_meta_data( '_ha_lead_time', (int) $raw );
} );
WooCommerce checks the nonce and the edit_post capability before it runs this hook, and saves the product object right after it, so update_meta_data() is enough; no extra save() is needed. WC_Admin_Meta_Boxes::add_error() shows the message above the product after the redirect. The older woocommerce_process_product_meta hook with update_post_meta() works as well, but it bypasses the product object, so other code that already loaded the product does not see the new value.
Show it on the product page
add_action( 'woocommerce_single_product_summary', function () {
global $product;
$days = (int) $product->get_meta( '_ha_lead_time' );
if ( $days > 0 ) {
echo '<p class="ha-lead-time">' . esc_html( sprintf(
_n( 'Ships in %d day', 'Ships in %d days', $days, 'textdomain' ),
$days
) ) . '</p>';
}
}, 25 );
Priority 25 places it below the price (10) and the excerpt (20). The leading underscore in the meta key hides the field from the generic Custom Fields box.
In the REST API
Meta set with update_meta_data() appears in meta_data of the product in the WooCommerce REST API (/wp-json/wc/v3/products/ID), and can be written the same way. Fields for single variations use different hooks; see custom fields for product variations.
NOTE: Products are still posts, so the value is stored in wp_postmeta; HPOS concerns only orders. If you need to sort or filter the shop by this value, a meta query on thousands of products gets slow; consider a product attribute instead, which WooCommerce can filter with its lookup table.