PHPwoocommerceintermediate
WooCommerce Custom Product Fields
Add custom fields to WooCommerce products with admin UI and frontend display
Faisal Yaqoob
December 23, 2025
#woocommerce#wordpress#product-fields#custom-fields#admin
Code
php
1 // Add custom fields to product general tab 2 add_action('woocommerce_product_options_general_product_data', 'add_custom_product_fields'); 3 function add_custom_product_fields() { 4 global $post; 5
6 echo '<div class="options_group">'; 7
8 // Custom text field 9 woocommerce_wp_text_input(array( 10 'id' => '_custom_product_text_field', 11 'label' => __('Custom Text Field', 'woocommerce'), 12 'placeholder' => 'Enter custom text', 13 'desc_tip' => 'true', 14 'description' => __('Enter a custom text value for this product.', 'woocommerce'), 15 )); 16
17 // Custom number field 18 woocommerce_wp_text_input(array( 19 'id' => '_custom_product_number_field', 20 'label' => __('Custom Number Field', 'woocommerce'), 21 'placeholder' => '', 22 'type' => 'number', 23 'custom_attributes' => array( 24 'step' => 'any', 25 'min' => '0' 26 ) 27 )); 28
29 // Custom select field 30 woocommerce_wp_select(array( 31 'id' => '_custom_product_select', 32 'label' => __('Custom Select', 'woocommerce'), 33 'options' => array( 34 '' => __('Select option', 'woocommerce'), 35 'option1' => __('Option 1', 'woocommerce'), 36 'option2' => __('Option 2', 'woocommerce'), 37 'option3' => __('Option 3', 'woocommerce'), 38 ) 39 )); 40
41 // Custom checkbox 42 woocommerce_wp_checkbox(array( 43 'id' => '_custom_product_checkbox', 44 'label' => __('Enable Custom Feature', 'woocommerce'), 45 'description' => __('Check this box to enable the custom feature.', 'woocommerce') 46 )); 47
48 echo '</div>'; 49 } 50
51 // Save custom fields 52 add_action('woocommerce_process_product_meta', 'save_custom_product_fields'); 53 function save_custom_product_fields($post_id) { 54 // Save text field 55 $custom_text = isset($_POST['_custom_product_text_field']) ? 56 sanitize_text_field($_POST['_custom_product_text_field']) : ''; 57 update_post_meta($post_id, '_custom_product_text_field', $custom_text); 58
59 // Save number field 60 $custom_number = isset($_POST['_custom_product_number_field']) ? 61 sanitize_text_field($_POST['_custom_product_number_field']) : ''; 62 update_post_meta($post_id, '_custom_product_number_field', $custom_number); 63
64 // Save select field 65 $custom_select = isset($_POST['_custom_product_select']) ? 66 sanitize_text_field($_POST['_custom_product_select']) : ''; 67 update_post_meta($post_id, '_custom_product_select', $custom_select); 68
69 // Save checkbox 70 $custom_checkbox = isset($_POST['_custom_product_checkbox']) ? 'yes' : 'no'; 71 update_post_meta($post_id, '_custom_product_checkbox', $custom_checkbox); 72 }
WooCommerce Custom Product Fields
Add custom fields to your WooCommerce products with a complete admin interface and frontend display functionality.
// Add custom fields to product general tab
add_action('woocommerce_product_options_general_product_data', 'add_custom_product_fields');
function add_custom_product_fields() {
global $post;
echo '<div class="options_group">';
// Custom text field
woocommerce_wp_text_input(array(
'id' => '_custom_product_text_field',
'label' => __('Custom Text Field', 'woocommerce'),
'placeholder' => 'Enter custom text',
'desc_tip' => 'true',
'description' => __('Enter a custom text value for this product.', 'woocommerce'),
));
// Custom number field
woocommerce_wp_text_input(array(
'id' => '_custom_product_number_field',
'label' => __('Custom Number Field', 'woocommerce'),
'placeholder' => '',
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
));
// Custom select field
woocommerce_wp_select(array(
'id' => '_custom_product_select',
'label' => __('Custom Select', 'woocommerce'),
'options' => array(
'' => __('Select option', 'woocommerce'),
'option1' => __('Option 1', 'woocommerce'),
'option2' => __('Option 2', 'woocommerce'),
'option3' => __('Option 3', 'woocommerce'),
)
));
// Custom checkbox
woocommerce_wp_checkbox(array(
'id' => '_custom_product_checkbox',
'label' => __('Enable Custom Feature', 'woocommerce'),
'description' => __('Check this box to enable the custom feature.', 'woocommerce')
));
echo '</div>';
}
// Save custom fields
add_action('woocommerce_process_product_meta', 'save_custom_product_fields');
function save_custom_product_fields($post_id) {
// Save text field
$custom_text = isset($_POST['_custom_product_text_field']) ?
sanitize_text_field($_POST['_custom_product_text_field']) : '';
update_post_meta($post_id, '_custom_product_text_field', $custom_text);
// Save number field
$custom_number = isset($_POST['_custom_product_number_field']) ?
sanitize_text_field($_POST['_custom_product_number_field']) : '';
update_post_meta($post_id, '_custom_product_number_field', $custom_number);
// Save select field
$custom_select = isset($_POST['_custom_product_select']) ?
sanitize_text_field($_POST['_custom_product_select']) : '';
update_post_meta($post_id, '_custom_product_select', $custom_select);
// Save checkbox
$custom_checkbox = isset($_POST['_custom_product_checkbox']) ? 'yes' : 'no';
update_post_meta($post_id, '_custom_product_checkbox', $custom_checkbox);
}
Display Custom Fields on Frontend
// Display custom fields on product page
add_action('woocommerce_single_product_summary', 'display_custom_product_fields', 25);
function display_custom_product_fields() {
global $product;
$custom_text = get_post_meta($product->get_id(), '_custom_product_text_field', true);
$custom_number = get_post_meta($product->get_id(), '_custom_product_number_field', true);
$custom_select = get_post_meta($product->get_id(), '_custom_product_select', true);
$custom_checkbox = get_post_meta($product->get_id(), '_custom_product_checkbox', true);
echo '<div class="custom-product-fields">';
if (!empty($custom_text)) {
echo '<p class="custom-text"><strong>Custom Info:</strong> ' . esc_html($custom_text) . '</p>';
}
if (!empty($custom_number)) {
echo '<p class="custom-number"><strong>Custom Number:</strong> ' . esc_html($custom_number) . '</p>';
}
if (!empty($custom_select)) {
echo '<p class="custom-select"><strong>Selected Option:</strong> ' . esc_html($custom_select) . '</p>';
}
if ($custom_checkbox === 'yes') {
echo '<p class="custom-feature"><span class="badge">Special Feature Enabled</span></p>';
}
echo '</div>';
}
Add Custom Fields to Inventory Tab
// Add custom fields to inventory tab
add_action('woocommerce_product_options_inventory_product_data', 'add_custom_inventory_fields');
function add_custom_inventory_fields() {
woocommerce_wp_text_input(array(
'id' => '_custom_supplier',
'label' => __('Supplier Name', 'woocommerce'),
'placeholder' => 'Enter supplier name',
'desc_tip' => true,
'description' => __('Enter the supplier name for this product.', 'woocommerce')
));
woocommerce_wp_text_input(array(
'id' => '_custom_reorder_point',
'label' => __('Reorder Point', 'woocommerce'),
'placeholder' => '10',
'type' => 'number',
'desc_tip' => true,
'description' => __('Stock level at which to reorder.', 'woocommerce')
));
}
Display in Admin Order Details
// Show custom fields in admin order items
add_action('woocommerce_before_order_itemmeta', 'display_custom_fields_in_admin_order', 10, 3);
function display_custom_fields_in_admin_order($item_id, $item, $product) {
if (!$product) {
return;
}
$custom_text = get_post_meta($product->get_id(), '_custom_product_text_field', true);
if ($custom_text) {
echo '<div class="custom-field-info">';
echo '<strong>Custom Info:</strong> ' . esc_html($custom_text);
echo '</div>';
}
}
Add to Cart Item Data
// Add custom field data to cart item
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_to_cart_item', 10, 3);
function add_custom_field_to_cart_item($cart_item_data, $product_id, $variation_id) {
$custom_text = get_post_meta($product_id, '_custom_product_text_field', true);
if (!empty($custom_text)) {
$cart_item_data['custom_field'] = $custom_text;
}
return $cart_item_data;
}
// Display custom field in cart
add_filter('woocommerce_get_item_data', 'display_custom_field_in_cart', 10, 2);
function display_custom_field_in_cart($item_data, $cart_item) {
if (isset($cart_item['custom_field'])) {
$item_data[] = array(
'name' => __('Custom Info', 'woocommerce'),
'value' => $cart_item['custom_field']
);
}
return $item_data;
}
Styling
.custom-product-fields {
margin: 20px 0;
padding: 15px;
background: #f9f9f9;
border-radius: 5px;
}
.custom-product-fields p {
margin: 8px 0;
}
.custom-product-fields .badge {
display: inline-block;
padding: 5px 10px;
background: #0073aa;
color: white;
border-radius: 3px;
font-size: 12px;
}
Features
- Easy Admin Interface: Uses WooCommerce's built-in field functions
- Multiple Field Types: Text, number, select, and checkbox fields
- Frontend Display: Shows custom fields on product pages
- Cart Integration: Displays custom data in cart and checkout
- Order Display: Shows custom fields in admin order details
- Extensible: Easy to add more custom fields
Dependencies
- WooCommerce
Related Snippets
WooCommerce Custom Checkout Fields
Add custom fields to WooCommerce checkout with validation and order display
PHPwoocommerceintermediate
phpPreview
// Add custom fields to checkout page
add_action('woocommerce_after_order_notes', 'custom_checkout_fields');
function custom_checkout_fields($checkout) {
echo '<div id="custom_checkout_fields"><h3>' . __('Additional Information') . '</h3>';
...#woocommerce#checkout#custom-fields+2
12/23/2025
View
WooCommerce Bulk Discounts and Quantity Pricing
Implement bulk discounts, quantity-based pricing, and tiered discounts in WooCommerce
PHPwoocommerceadvanced
phpPreview
// Apply bulk discount based on quantity
add_action('woocommerce_before_calculate_totals', 'apply_bulk_discount');
function apply_bulk_discount($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
...#woocommerce#pricing#discounts+2
12/23/2025
View
WooCommerce Custom Email Notifications
Create custom email notifications for WooCommerce events and triggers
PHPwoocommerceadvanced
phpPreview
// Create custom email class
if (!class_exists('WC_Custom_Order_Email')) {
class WC_Custom_Order_Email extends WC_Email {
...#woocommerce#email#notifications+2
12/23/2025
View